Computing.Net > Forums > Unix > grep to find a specific line number

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

grep to find a specific line number

Reply to Message Icon

Name: u9717410
Date: September 22, 2004 at 02:32:56 Pacific
OS: hp ux 11
CPU/Ram: unknown
Comment:

I am attempting to find a particualr line in a file and then replace it. Unfortunatly i can only guaruntee the content of the line prior to the one i am trying to edit. I have used grep -n and a cut to get the line number of the previous line and i now want to be able to return the line after this one. I know part of the string but this returns several lines i have tried:
grep -n $string $file | grep $line_number: $file
where $line_number is the previous line number + 1 and $string is the only consistent part of the line but this returns nothing.

Can someone suggest a solution using grep and if not maybe using awk.



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: September 22, 2004 at 07:00:31 Pacific
Reply:

Here are two sed solutions and one awk solution.  Of course, sed and awk both can identify the line prior using pattern match instead of exact match.

sed '/the line prior/{N;s/\(.*\n\).*/\1mynewline/;}' myfile

sed '/the line prior/{x;N;s/.*/mynewline/;x;G;}' myfile

awk '{
print
if ($0=="the line prior")
   {getline
    print "mynewline"}
}' myfile


0

Response Number 2
Name: NC_Jed
Date: October 26, 2004 at 08:36:17 Pacific
Reply:

grep -n "term to find" file.txt | cut -f1 -d:

also works nicely. I use it in Bash scripts to set variables to pass to sed deletes.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: grep to find a specific line number

extract a specific line from file www.computing.net/answers/unix/extract-a-specific-line-from-file/5381.html

Cat, viewing specific line number www.computing.net/answers/unix/cat-viewing-specific-line-number/4544.html

removing specific line from file www.computing.net/answers/unix/removing-specific-line-from-file/5042.html