0

I have many files, from file0 to file3100. I want to add ### for one line in each file.

The line

Generated by trjconv : PTEG1 t= 8100.00000

should be

###Generated by trjconv : PTEG1 t= 8100.00000

The time (t) in each file is different. How can I add ### to each file in once and the time (t) is still same in each file as before adding?

Here is what I tried:

#!/bin/bash

# loop all inp files in the current dir
for frameFile in frame*.inp; do
    sed -i -r -e 's/Generated by trjconv : pteg-1 t=.+/###Generated by trjconv : pteg-1 t=.+/g' ${frameFile}
fi
done
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • update your question with input lines from several files – RomanPerekhrest Sep 12 '17 at 11:48
  • this is line before: Generated by trjconv : PTEG1 t= 900.00000 harmon , this is what it should be : # # # Generated by trjconv : PTEG1 t= 900.00000 harmon – Abuzer Alhamza Sep 12 '17 at 11:55
  • the problem is that you need to compare all files on `t`(time) equality beforehand – RomanPerekhrest Sep 12 '17 at 12:14
  • yes, that the time doesn't change time in each files. – Abuzer Alhamza Sep 12 '17 at 12:47
  • i have this script, but it does't work #!/bin/bash # loop all inp files in the current dir for frameFile in frame*.inp do sed -i -r -e 's/Generated by trjconv : pteg-1 t=.+/###Generated by trjconv : pteg-1 t=.+/g' ${frameFile} fi done – Abuzer Alhamza Sep 12 '17 at 12:58
  • I've added the script you mentioned in your comment. Please [edit] your question instead of posting essential information in comments. If I got it right, it looks like you have an unneeded `fi`. You should describe what you mean by "it doesn't work". What happens? What do you expect should happen instead? – Benjamin W. Sep 12 '17 at 21:28
  • thank you, it works now! – Abuzer Alhamza Sep 14 '17 at 08:11

1 Answers1

0

A simple perl one liner does the job:

perl -pi -e 's/Generated by trjconv : PTEG1 t=/###$&/' file*

This will replace Generated by trjconv : PTEG1 t= with ###Generated by trjconv : PTEG1 t= within all files that have names begining with file

Toto
  • 89,455
  • 62
  • 89
  • 125