Results 1 to 7 of 7

Thread: split string

  1. #1
    Join Date
    Jan 2005
    Posts
    8

    Unanswered: 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

  2. #2
    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?

  3. #3
    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> |
    +-----------------------+

  4. #4
    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

  5. #5
    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> |
    +-----------------------+

  6. #6
    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?

  7. #7
    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> |
    +-----------------------+

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •