Reputation: 3
So my problem is as follows: I have a bunch of files and i need only the information from a certain line in each of these files (the same line for all files). Example: I want the content of the line 10 from file example_1.dat~example_10.dat and then i want to save it on > test.dat
I tried using: head -n 5 example_*.dat > test.dat
. But this gives me all the information from the top till the line i have chosen instead of just the line.
Please help.
Upvotes: 0
Views: 250
Reputation: 58381
This might work for you (GNU sed):
sed -sn '5wtest.dat' example_*.dat
Upvotes: 0
Reputation: 26
$ for f in *.dat ; do sed -n '5p' $f >> test.dat ; done
This code will do the following:
Foreach file f in the directory that ends with .dat. Use sed on the 5:th row in file and write to test.dat. The ">>" will add the row at the bottom of the file if existing.
Upvotes: 1
Reputation: 21435
Use a combination of head
and tail
to zoom to the needed line. For example, head -n 5 file | tail -n 1
You can use a for
loop to get it done over several files
for f in *.dat ; do head -n 5 $f | tail -n 1 >> test.dat ; done
PS: Don't forget to clean the test.dat
file (> test.dat
) before running the loop. Otherwise you'll get results from previous runs as well.
Upvotes: 1