display one line from multiple files
I have 500 files that I have to look through but only interested in 20th line. Is there any way for me to view the 20th line of all of the 500 files at the same time?
I've been using head -n 20 filename | tail -n 1 but then I have to do this 500 times... Thank you for the help! |
If they are all in same directory you could do:
Code:
for i in `ls -1 /directory` |
Hi,
Are all the files in the same directory? If so: Code:
for THISFILE in `ls`; do sed -n '21q;20p' $THISFILE; done Code:
for THISFILE in `ls`; do echo -n "$THISFILE : " ; sed -n '21q;20p' $THISFILE; done |
Using awk:
Code:
awk 'FNR==20' * Code:
awk 'FNR==20{print FILENAME ":", $0; nextfile}' * |
I started with nine files.
Code:
test$ ls Code:
test$ cat file_1 Code:
test$ awk 'FNR == 20' file_* Code:
test$ find -maxdepth 1 -name 'file_*' -exec awk 'NR == 20 {print FILENAME ":" $0}' {} \; Code:
test$ find -maxdepth 1 -name 'file_*' -exec awk 'NR == 20 {print FILENAME ":" $0}' {} \; | sort Code:
test$ find -name 'file_*' -exec awk 'NR == 20 {print FILENAME ":" $0}' {} \; | sort HTH :) EDIT colucix made a good point about accelerating awk by using nextfile to ignore all lines after line 20. If I incorporate that into my last example I get this. Code:
test$ find -name 'file_*' -exec awk 'NR == 20 {print FILENAME ":" $0; nextfile}' {} \; | sort |
Quote:
|
The proverbial sed-based one:
Code:
find <base_dir> -type f -exec sed -n '20{p;q}' {} \; |
All times are GMT -5. The time now is 07:59 AM. |