The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using grep to extract line number mskarica Shell Programming and Scripting 8 06-25-2008 11:47 PM
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 05:29 AM
Extract a line from a file using the line number zambo Shell Programming and Scripting 1 05-01-2008 10:39 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-20-2007 10:29 PM
Get line form file by line number corny Shell Programming and Scripting 3 08-25-2006 09:18 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-02-2006
Registered User
 

Join Date: Jul 2006
Posts: 8
extract a line from a file using the line number

Hello,
I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4).
Extra Notes:
-I am working in a bash script.
-The file size is always variable, so I couldn't use head or tail.
#file that I am evaluating
Code:
hello
hey
hi
there <-line that i want
how
are
you 
doing
Reply With Quote
Forum Sponsor
  #2  
Old 08-02-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Use sed. Check the 'p' command in particular.
Reply With Quote
  #3  
Old 08-02-2006
System Shock's Avatar
Registered User
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 395
If you want a particular line number, say line 4, using head and tail commands:
Code:
 head -4 filename | tail -1
Using sed:
Code:
 sed 4!d filename
Reply With Quote
  #4  
Old 08-02-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
You can use awk

awk 'NR==4{print $0}' filename

would print the 4th line for you.
Reply With Quote
  #5  
Old 08-03-2006
Registered User
 

Join Date: Jul 2006
Posts: 8
Thank you everyone for responding. I went with the head to tail command. I previously didn't think the head/tail command would work but thank you for showing me the correct usage.
Reply With Quote
  #6  
Old 08-03-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
No need to explicitly print in the awk solution, it can simply be: awk 'NR==4' filename
Reply With Quote
  #7  
Old 02-06-2008
Registered User
 

Join Date: Nov 2006
Posts: 1
Quote:
Originally Posted by grandtheftander View Post
Hello,
I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4).
Extra Notes:
-I am working in a bash script.
-The file size is always variable, so I couldn't use head or tail.
#file that I am evaluating
Code:
hello
hey
hi
there <-line that i want
how
are
you 
doing

head -4 | awk '{ print $1 }' < filename | tail -1
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0