extract nth line of all files in a directory

July 6, 2010 at 19:49:45
Specs: UNIX
Hello Unix Experts,

I have 123 large data files in a directory. I want to extract the line number 44596 from each file sequentialy and print those lines on seperate lines in the new output file.
note that the filenames are 2000.02_G3.input.txt, 2000.03_G3.input.txt,.....,2010.04_G3.input.txt
But some files are missing in between.

See More: extract nth line of all files in a directory

July 7, 2010 at 07:07:13
Use sed to print a range of lines.

# untested
sed -n '44596p' *.input.txt > new.file

I have never accessed files this large. Let me know if this doesn't work.

You could also use awk:

# untested
awk ' { if(NR == 44596)
print $0 } ' ....

Report •

July 7, 2010 at 07:15:27
Thank you nails....
This works fine for a single file.
But I want to write a shell script which can print line number 44596 of each file in the directory into the output file on seperate lines.

for example, input files are one.txt, two.txt, three.txt
and the cat of these files are
one.txt two.txt three.txt
11111 222222 3333333
11111 222222 3333333
11111 222222 3333333
respectively. So, the desired output should be:
cat of output.txt ( it should print the 3rd line of each file on seperate lines of output.txt)

Thankx in advance.

Report •

July 7, 2010 at 09:04:43
Interesting. It looks like the sed command doesn't recognize the wild card. Try something like this:

# untested
# first, make sure your script changes to your directory
cd <your directory>

rm new.file
for i in *.input.txt 
   sed -n '44596p' "$i" >> new.file

Report •
Related Solutions

Ask Question