If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > split string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-17-05, 21:44
izza_azhar izza_azhar is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
split string

if file name such:
ATK_7B1_WIP_20041116160700.9.xml.dat
ATK_7B1_WIP_20041116160700.93474039283928384.xml.d at
so how can i cut to only get the year,month and year only

such:1) 200411
2) 2004
Reply With Quote
  #2 (permalink)  
Old 01-17-05, 22:15
izza_azhar izza_azhar is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
split string

let say my program such this:

for FILE in ls ATK*.dat
do
curr_year=$(date +%Y)
curr_month=$(date +%Y%m)
file_year=<filename just have the year>
file_month=<filename that just have year and month>

if [ "$curr_year" = "$file_year" ]
then
if [ 'expr $curr_month - $file_month >= 3 ]
then
mv $FILE /u006/r45647/Izza/xsltOutput/archiveprocess
<move the file to another directory>

fi

fi

done

can we use sen to pass the variable for $file_year or file month?
or it's easy use the cut only?
Reply With Quote
  #3 (permalink)  
Old 01-17-05, 22:19
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
yearMonth:
echo 'ATK_7B1_WIP_20041116160700.9.xml.dat' | sed -e 's/.*_\([^_]\{6,6\}\).*/\1/g'

year:
echo 'ATK_7B1_WIP_20041116160700.9.xml.dat' | sed -e 's/.*_\([^_]\{4,4\}\).*/\1/g'
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #4 (permalink)  
Old 01-17-05, 22:43
izza_azhar izza_azhar is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
ok

it done well if i dont pass the variable..
but, how to do that?

file_year=$FILE | sed -e 's/.*_\([^_]\{6,6\}\).*/\1/g'

it cannot work..
coz i need to use back the valu to make comparison
please
Reply With Quote
  #5 (permalink)  
Old 01-17-05, 22:52
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
file_year=`echo "${FILE} | sed -e 's/.*_\([^_]\{4,4\}\).*/\1/g'`
file_month=`echo "${FILE} | sed -e 's/.*_\([^_]\{6,6}\).*/\1/g'`
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #6 (permalink)  
Old 01-18-05, 01:16
izza_azhar izza_azhar is offline
Registered User
 
Join Date: Jan 2005
Posts: 8
ok...

the program is like this actually:

for FILE in ls ATK*.dat
do
curr_year=$(date +%Y)
#curr_month=$(date +%Y%m)
curr_month=200412<just for example>
file_year=`echo "${FILE} | sed -e 's/.*_\([^_]\{4,4\}\).*/\1/g'`
file_month=`echo "${FILE} | sed -e 's/.*_\([^_]\{6,6}\).*/\1/g'`

if [ "$curr_year" = "$file_year" ]
then
if [ `expr $curr_month - $file_month` -ge 3 ]
then
mv $FILE /u006/r45647/
echo "this $FILE moved to r45647 directory"
fi
else
echo " please make sure all file names"

fi

done

i still cannot used the sed...
can it be done that way?
Reply With Quote
  #7 (permalink)  
Old 01-18-05, 09:08
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
seems like you're using ksh - make sure you specify the interpreter at the top of the file.
Also be careful copy/pasting the code and indent the code appropriately.
There's no need for 'ls ATK*.dat' - this is UUOL [Useless Use Of Ls].

Here's something to try:
Code:
#!/bin/ksh
#set -x

#file='ATK_7B1_WIP_20051116160700.9.xml.dat'
#for FILE in "${file}"

for FILE in ATK*.dat
do
   curr_year=$(date +%Y)
   curr_month=$(date +%Y%m)
   #curr_month=200412 #<just for example>
   file_year=$(echo ${FILE} | sed -e 's/.*_\([^_]\{4,4\}\).*/\1/g')
   file_month=$(echo ${FILE} | sed -e 's/.*_\([^_]\{6,6\}\).*/\1/g')

   if (( $curr_year == $file_year ))
   then
       if (( ($curr_month - $file_month) >= 3 ))
       then
          mv $FILE /u006/r45647/
          echo "this $FILE moved to r45647 directory"
       fi
   else
       echo 'please make sure all file names'
   fi
done;
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On