Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

any unix built in command to print only desire line from file?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,
Does anyone know of any built in unix command that can print any single line that the user wants from a file?
File exceeds 100 lines, lines are separated with newline character '\n'.

I need to extract certain line within the file. Have tried inserting line at beginning of each line then grepping the current number and cut the second field, but the problem is grep is having problem when file exceeds 100 lines, grepping for $I (ex. where $I=1) would return 1, 10,100 101 and so on.

Any help is greatly appreciated.
Thank you.
 
Hi, not sure whether thread822-246994 will help you, but if not, try a search for 'lines' in this forum - there seem to be plenty of people doing variants of what you want. HTH.
 
Hi Phuc,

Instantaneously I am remembered of the 'sed'
(stream editor)command. You can either pick a single line or pick a batch of lines using this command. For eg:-
Say you have a file of 20 lines.

1. To pick say the 5th line try this:-
sed -n 5p <filename>

2. To pick say 5th,6th and the 7th lines try this:-
sed -n 5,7p <filename>

Don't forget to include the '-n' option because 'sed' normally outputs every line of its input file to the console. By including this option you get only the required number of lines (lines filtered from the input file by your conditions). If you want to pick lines at run time or dynamically include the necessary lines in a script and pass the line number you want to pick as an argument and substitute it in the 'sed' command suitably.

There are roundabout ways of doing what you want but I guess this is one of the better ways. Hope the above explanation answers your question.
 
Thank you Saikrish and Ken for the help.

This is exactly what I wanted it do, I will fix up my script and see what happen. I was looking through the man pages for sed but didn't know that -n #p will print only that line number. All the man pages had is '-n --quiet'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top