Operating System - HP-UX
1832220 Members
6387 Online
110041 Solutions
New Discussion

extracting specific line from text

 
SOLVED
Go to solution
Henry Chua
Super Advisor

extracting specific line from text

Hi Guys,

Say I want to extract line 2 from a text log, what is the best way to do it using shell?
Thanks

Best regards
Henry
5 REPLIES 5
Alex Lavrov.
Honored Contributor
Solution

Re: extracting specific line from text


head -2 | tail -1
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Peter Godron
Honored Contributor

Re: extracting specific line from text

Henry,
you do not specify the selection criteria for your extract (top of the file,end of the file....)

If you want to extract lines 12 and 13 of a file for example you can use
sed -n '12,13' file.lis

For top 2 lines:
head -2 file.lis

For last 2 lines:
tail -2 file.lis

Can you please update with more details.
Regards
Alex Lavrov.
Honored Contributor

Re: extracting specific line from text

Another one I came up with:

cat -n | awk '$1 ~ ln {print $0}' ln=

For example:

cat -n /tmp/my_test.txt | awk '$1 ~ ln {print $0}' ln=28
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Indira Aramandla
Honored Contributor

Re: extracting specific line from text

Hi Henry,

To list lines 2 through 8 of a file

In awk ----> awk 'NR>=2 && NR <=8'

In sed ----> sed -n -e 2,8p filename

In perl ----> perl -ne 'print unless (($.<2) or ($.>8));' filename

In your case to extract line 2

sed -n -e 2,2p filename

IA
Never give up, Keep Trying
Henry Chua
Super Advisor

Re: extracting specific line from text

Thanks guys, got the answer I want..
Alex first solution is brilliant.. short but handy..

THanks!!