Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

grep to find a specific line number

Original Message
Name: u9717410
Date: September 22, 2004 at 02:32:56 Pacific
Subject: grep to find a specific line number
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.


Report Offensive Message For Removal


Response Number 1
Name: Jim Boothe
Date: September 22, 2004 at 07:00:31 Pacific
Subject: grep to find a specific line number
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 2
Name: NC_Jed
Date: October 26, 2004 at 08:36:17 Pacific
Subject: grep to find a specific line number
Reply: (edit)
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.


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: grep to find a specific line number

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




My PC has been hijacked!

Lexmark 2600 Printer Issues

btk1w1 infected start here post

Unwanted message remians on screen

Slow boot time


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC