Sunday, December 5, 2010

How to use sed to extract lines from a text file

In a text file, every 1090 lines corresponds to solution at one time step. I want to extract one solution every 100 time step, what should I do?

This can be done easily using the Linux tool "sed".

First, we extract the solutions and save it into one single file:
sed -n 1~109000,+1089p test.txt > out.txt
Here "1" means the starting line of the text file, "~109000" means every 109000 lines, "+1089" means print that line and the following 1089 lines.

Next, we can split the solutions in out.txt into seperate text files using the Linux tool "split".
split -l 1090 -a 6 -d out.txt new
Here "-l 1090" means every 1090 lines save as a new file, "-a 6" means to use 6 digits in the numbering of the file names, "-d" means to use digits instead of letters in the numbering, the final "new" means the files names will start with "new".

For more options, you can use "man sed" and "man split" to look up the manual.

No comments:

Visitors