Changing last line in many files in sed - sed only change in first

Viewed 61

This is my script

#!/bin/bash
for num in {1..100}; 
do 
    sed '$s/ 6.36535    23.3762512.09434/   6.76889  21.76071  12.19032/' eq8_$num.gro | tee eq9_$num.gro
done

I want to replace " 6.36535 23.3762512.09434" to " 6.76889 21.76071 12.19032" (white space are important in this case, files have different number of lines, and this is the last line of each file).

Sed work only on the first file. Input example

eq8_1.gro

900 mgdg molecules in water t= 600000.00000 step= 400000000
177255
  227BGL     C529155   4.134  15.788   5.556
  227BGL     H529156   4.186  15.879   5.529
19144SOL     OW73518   0.911  13.325   6.427
19144SOL    HW173519   0.879  13.256   6.485
20389SOL     OW77253   0.644  14.144   5.376
20389SOL    HW177254   0.712  14.203   5.344
 6.36535    23.3762512.09434

eq8_2.gro

900 mgdg molecules in water t= 600000.00000 step= 400000000
177255
19922SOL    HW175853   4.186  14.169   5.941
19922SOL    HW275854   4.071  14.076   5.973
19924SOL     OW75858   1.466  20.721   5.803
19924SOL    HW175859   1.489  20.642   5.852
 6.36535    23.3762512.09434

Output examples

eq9_1.gro

900 mgdg molecules in water t= 600000.00000 step= 400000000
177255
  227BGL     C529155   4.134  15.788   5.556
  227BGL     H529156   4.186  15.879   5.529
19144SOL    HW173519   0.879  13.256   6.485
19144SOL    HW273520   1.007  13.318   6.431
20389SOL    HW177254   0.712  14.203   5.344
20389SOL    HW277255   0.577  14.201   5.413
   6.76889  21.76071  12.19032

eq9_2.gro (last line dont change, why?)

900 mgdg molecules in water t= 600000.00000 step= 400000000
177255
19922SOL     OW75852   4.097  14.168   5.977
19922SOL    HW175853   4.186  14.169   5.941
19924SOL     OW75858   1.466  20.721   5.803
19924SOL    HW175859   1.489  20.642   5.852
 6.36535    23.3762512.09434

I also try different method

#!/bin/bash
for num in {1..100}; 
do 
    sed -i '$s/ 6.36535    23.3762512.09434/   6.76889  21.76071  12.19032/' eq8_$num.gro
done

It doesnt work too.

If you dont know how to do this in sed, maybe you know how to do this in awk?

3 Answers

This might work for you (GNU sed and parallel):

parallel -q sed -s '$s/ 6.36535    23.3762512.09434/   6.76889  21.76071  12.19032/' ::: eq8_{1..100}.gro

This will apply the above sed substitution command to the last line of 100 file from eq8_1.gro to eq8_100.gro.

The output will got to stdout and can be captured > outFile.

N.B. The original files will not be amended.

If the original files need to be amended in place, use:

parallel -q sed -i.backup '$s/ 6.36535    23.3762512.09434/   6.76889  21.76071  12.19032/' ::: eq8_{1..100}.gro

To leave the original files in place and generate new files with the amendments, use:

parallel sed -s \''$s/ 6.36535    23.3762512.09434/   6.76889  21.76071  12.19032/'\' \> {.}.new.txt ::: eq8_{1..100}.gro

You might need to ensure there is a newline character at the end of the file. sed implementations are typically picky about the meaning of $. Consider e.g.

sed -i.bak -e '$a\' file

or even better if your shell allows it:

[ -n "$(tail -c1 file)" ] && printf '\n' >>file 

Did you try a sed -i 's/Pattern1/Pattern2/g' the g in the end does a global replace on all matched patterns in the file.

Related