0

I am trying to do post processing using a bash script. One of the step is to copy from lines below certain line of the file to another file. For example, my file result.txt looks like this

6798
Atoms. Timestep: 0
1 -1.13977 -2.85824 -1.22655
1 -1.20925 -3.25439 -0.978641
1 -1.54565 -2.93301 -1.10555
1 -0.736167 -2.71201 -1.28063
1 -0.807178 -3.16856 -1.13489
1 -0.44354 -3.03183 -1.23103
1 -0.357782 -3.39866 -1.0277
1 -0.0431648 -3.05734 -1.23315
6798
Atoms. Timestep: 1
1 0.119814 -3.40483 -1.03653
1 0.382198 -3.03771 -1.23183
1 0.580509 -3.37434 -1.02215
1 0.818628 -3.00063 -1.21422
1 1.0244 -3.31469 -0.980957
1 1.27482 -2.97232 -1.15564

I want only the part below Atoms. Timestep: 1 . It is very easy to do it manually, however, there are thousands of file like that so that i want to use bash script to do it for me. I have checked several method from google however, they are not that fit from my question. If you have experience in that it would be nice to post your result below. That may help others who have similar problem like me.

Thank you in advance!

FINAL SOLUTION

My final solution is as guys mentioned below, I make a conclusion.

sed -n '/Atoms. Timestep: 1/, $ p' < result.txt > resultParsed.txt
sed -i '/Atoms. Timestep: 1/d' resultParsed.txt

The first line will get the content including and after Atoms. Timestep: 1. The second line will delete the line that you don't need.

Lbj_x
  • 415
  • 5
  • 15
  • 3
    so from `Atoms. Timestep: 1` up to the end of the file? Something like [How to get the part of file after the line that matches grep expression ? (first match)](http://stackoverflow.com/q/7103531/1983854)? – fedorqui Jun 03 '15 at 13:25
  • 1
    @fedorqui thank you for the reminding, I will update my final solution in my post – Lbj_x Jun 03 '15 at 17:44

2 Answers2

2

This will copy from result.txt, from the first match until the end of the file and save in resultParsed.txt:

sed -n '/Atoms. Timestep: 1/, $ p' < result.txt > resultParsed.txt

This will copy from result.txt, from the first match and the next 6 entries and save in resultParsed.txt:

sed -n '/Atoms. Timestep: 1/ , +6 p' < result.txt > resultParsed.txt

Environment: GNU sed version 4.1.5

  • 1
    In both cases, you can avoid a useless use of `cat` by taking input straight from the file: `sed -n '/Atoms\. Timestep: 1/,$p' resultParsed.txt` – Toby Speight Jun 03 '15 at 14:01
  • 1
    The question asked only for the part *below* the timestep 1 line, so a better choice would be `sed -e '1,/Timestep: 1$/d'` to remove that line as well. – Toby Speight Jun 03 '15 at 14:03
1

Using awk

awk 'x+=/Atoms\. Timestep: 1/' file >newfile

Increments x when that string if found, x is then more than zero which is seen as true and the default action for awk is to print. So all lines including and after this one are printed.

123
  • 10,778
  • 2
  • 22
  • 45