![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Use sed. Check the 'p' command in particular.
|
|
#3
|
||||
|
||||
|
If you want a particular line number, say line 4, using head and tail commands:
Code:
head -4 filename | tail -1 Code:
sed 4!d filename |
|
#4
|
|||
|
|||
|
You can use awk
awk 'NR==4{print $0}' filename would print the 4th line for you. |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
|||
|
|||
|
No need to explicitly print in the awk solution, it can simply be: awk 'NR==4' filename
|
|
#7
|
|||
|
|||
|
Quote:
head -4 | awk '{ print $1 }' < filename | tail -1 |
|||
| Google The UNIX and Linux Forums |