sed isn't sedding


I wrote a little bash script to change something in some tab delimited text files based on a value taken from the filename. This is the script.

[COLOR="SeaGreen"]# get list of new output files from pwd[/COLOR]
NEW_FILES=$(ls *'out.txt')

[COLOR="SeaGreen"]# process new output files to rename and move[/COLOR]

[COLOR="SeaGreen"]   # create new file name to add epochs already processed to min mae epoch number
   # parse original file name to array using with "_", "E", or "-" as delimiter[/COLOR]
   IFS='_-E' read -a FIELD <<< "$NEW_FILE"

[COLOR="SeaGreen"]### not wokring
    # change the epoch values in the file to add epochs already processed
    # create a list of the epoch values in the current file[/COLOR]
    EPOCH_VALUE_LIST=$(grep -oh "E[0-9][0-9]*" $NEW_FILE)

       echo " "
       echo "correcting"
       echo $NEW_FILE
       [COLOR="SeaGreen"]#remove the leading E[/COLOR]
       [COLOR="SeaGreen"]#create a new epoch value[/COLOR]
       [COLOR="SeaGreen"]# substitute in file[/COLOR]
   [COLOR="SeaGreen"]# change out E_A back to E and overwrite original file[/COLOR]
   sed "s/E_A/E/g" $NEW_FILE'_TEMP.txt' > $NEW_FILE
   [COLOR="SeaGreen"]# cleanup temp file[/COLOR]
   rm $NEW_FILE'_TEMP.txt'

This is an example of a filename,
the number in blue bold is what is parsed to ${FIELD[8]} by the read -a operation.

Within each file are two rows of tab delimited columns with values like,
E100  E200  E300  E400  E500
There can be any maximum value. The task of the script is to take the value from the file name and add it to each of the E* fields. In the filename above, the values in the output file should look like,
E300  E400  E500  E600  E700
with 200 added to each.

The above script is a bit or a hack, but it should work. The output gives each E* value and the E*+200 value that it should be replaced with. There is an output file written, but it is unchanged from the input except for the last E* value, which has been correctly modified. The first sed operation does not appear to be matching to the E* values in the list $EPOCH_VALUE_LIST, except for the last one. Since these values print correctly, it's hard to see what the issue is. It's also hard to see why this works for the last column only.

I have attached the script and a directory of test input files. The script can just be run from inside the test directory. I am stuck at this point, so suggestions would be greatly appreciated.

Sign In or Register to comment.