2011年3月20日星期日

unix commands - display / read / print out a specific line of a file

# line number is the number of line you want
sed -n "${line_number}p" $filename

# here the number 2 can be substitute with the number of line you want,
# tail -1 control how to print out the line, lines around the specific one.
cat filenames | head -2 | tail -1
head -2 filenames | tail -1
head -n 2 filenames | tail -1
sed -n 2p filenames

# print out a range of lines by line-numbers
awk 'FNR>=20 && FNR<=40' filename awk 'FNR>=101264075 && FNR<=101264080' filename

# zcat for displaying the compressed file
zcat filename | head -101264077 | tail -5

zcat filename | head -99327817 | tail -5

# print the 5th line of file file1.txt
sed -n '5 p' file1.txt

没有评论:

发表评论