Find the answer to your Linux question:
Results 1 to 3 of 3
Was wondering if anyone could point me in the right direction here..I have a script that is supposed to append a batch number on the end of a file (before ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    6

    Thumbs up basic sed regex backreferencing q

    Was wondering if anyone could point me in the right direction here..I have a script that is supposed to append a batch number on the end of a file (before the extension), but I don't think I have the syntax right. For example, if I have a file called:
    FILE_Z66_ALPHA.txt
    I want the script to change it to the following:
    FILE_Z66_ALPHA_BATCH_1.txt

    I'm trying to use sed with backreferencing but I don't think my syntax is correct ... does anyone have any suggestions?

    #!/bin/bash

    for filename in `ls *.txt`
    do
    new_filename=`echo $filename | sed -r s/\([\S]*\).txt/\1_BATCH_1.txt/`
    echo $new_filename
    mv $filename $new_filename

    done

    Thanks !

  2. #2
    Just Joined!
    Join Date
    Mar 2009
    Posts
    31
    not sure if this will help much but depending on your file names. if you can guarantee there will only be one '.' in the filename and there are no spaces, you could use this regular expresion

    new_filename=`echo $filename | sed 's/\./_BATCH_1\./'`

    - John

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    # a=FILE_Z66_ALPHA.txt
    # IFS="."
    # set -- $a
    # echo ${1}_01.${2}
    FILE_Z66_ALPHA_01.txt
    #unset IFS

Posting Permissions

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