sed isn't sedding

Hello,

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"]#!/bin/bash[/COLOR]

[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]
for NEW_FILE in $NEW_FILES
do

[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)

    for EPOCH_STRING in $EPOCH_VALUE_LIST
    do
       echo " "
       echo "correcting"
       echo $NEW_FILE
       echo "EPOCH_STRING" $EPOCH_STRING
       [COLOR="SeaGreen"]#remove the leading E[/COLOR]
       EPOCH_VALUE=${EPOCH_STRING#E}
       [COLOR="SeaGreen"]#create a new epoch value[/COLOR]
       NEW_EPOCH_STRING="E_A$(($EPOCH_VALUE + ${FIELD[8]}))"
       echo "NEW_EPOCH_STRING" $NEW_EPOCH_STRING
       [COLOR="SeaGreen"]# substitute in file[/COLOR]
       sed "s/$EPOCH_STRING/$NEW_EPOCH_STRING/g" $NEW_FILE > $NEW_FILE'_TEMP.txt'
   done
   [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'
done

This is an example of a filename,
73.22_E1300_73.74_E1600_f0_r17_200-ON-0.25_3500-ON-0.1lr_S7A_v4_47.19.1.out.txt
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.

LMHmedchem
Sign In or Register to comment.