The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep problem with the ^ symbol, help needed please. Gvsumrb UNIX for Dummies Questions & Answers 2 03-31-2008 07:04 AM
SED command ---------help needed veerapureddy Shell Programming and Scripting 4 03-17-2008 01:12 PM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-04-2007 11:59 PM
Help with tail /grep needed Felix2511 UNIX for Dummies Questions & Answers 4 05-17-2007 12:22 PM
help needed for sed command manualvin UNIX for Dummies Questions & Answers 2 06-24-2004 08:41 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-10-2007
Registered User
 

Join Date: May 2006
Posts: 63
Help Needed in Grep Command

Hi

i am using grep command to search a string in a file and i am able to find that string in it ..

but moving forward once i find the string i need to remove the 5 lines above that and to remove 2 lines below it .. Just wanted to be clear there should be no lines existing which has a string coys11

Example :

file1.txt
cat435
gggte
werwr
coys11
ewrer

when i run this script , it should delete all the lines above it and lines below it

Thanks.. Please do help me on this .. Thanks again..!!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 09-10-2007
Registered User
 

Join Date: May 2006
Posts: 63
Help Needed in Grep Command

Hi

i am using grep command to search a string in a file and i am able to find that string in it ..

but moving forward once i find the string i need to remove the 5 lines above that and to remove 2 lines below it .. Just wanted to be clear there should be no lines existing which has a string coys11

Example :

file1.txt
cat435
gggte
werwr
coys11
ewrer

when i run this script , it should delete all the lines above it and lines below it

Thanks.. Please do help me on this .. Thanks again..!!
Reply With Quote
  #3 (permalink)  
Old 09-10-2007
Registered User
 

Join Date: Feb 2007
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 187
Hey
why dont you use sed ?
sed '/^[Cc]oys11/p' Filename

Hope this is what you want !!

Thanks !
Reply With Quote
  #4 (permalink)  
Old 09-10-2007
Registered User
 

Join Date: Aug 2007
Posts: 45
Quote:
Originally Posted by varungupta View Post
Hey
why dont you use sed ?
sed '/^[Cc]oys11/p' Filename

Hope this is what you want !!

Thanks !
I thing this one removes not just "coys11" it removes all the line which inherits c,o,y,s,1.

if he just want to remove just coys11
grep -v -w joys11 inpufile is better. But I thing he wants something else
Reply With Quote
  #5 (permalink)  
Old 09-10-2007
Registered User
 

Join Date: Aug 2007
Posts: 45
let's say your file looks like this;
.
.
.
aa
bb
cc
dd
ee
ff
gg
hh
ii
jj
kk
.
.

and let's say your string is gg
do you want to remove bb-ii (including bb,gg,ii) ? if it is;

currentline=`grep -n string inputfile|awk ' {print $1} '`
let "startingline=$currentline-5"
let "finishingline=$currentline+2"

sed -n "$startingline,${finishingline}!s/.*/&/p" inputfile>outfile
Reply With Quote
  #6 (permalink)  
Old 09-10-2007
Registered User
 

Join Date: May 2006
Posts: 63
Hi

Thanks for the replies

Just wanted to be clear

lets assume i have a file1.txt which has the following

aaa
bbb
bbb
ccc
qqq
343
454
eee

i am looking for a string 343 once the script finds 343 it should delete 4 lines above it and 2 lines below it

the output file should be

qqq

Thanks
Reply With Quote
  #7 (permalink)  
Old 09-11-2007
Registered User
 

Join Date: Aug 2007
Posts: 45
is there any line before aaa and after eee in the file ?

if there is not than the code is;

let "currentline=`grep -n string inputfile|awk -F ':' {print $1} '`-1"
sed -n "${currentline}s/.*/&/p" inputfile>outfile

if there is. it is a bit harder .The code is ;

let "currentline=`grep -n string inputfile|awk -F ':' ' {print $1} '`-1"
let "fisrtpart=$currentline-5"
let "secondpart=$currentline+4"
sed -n "1,${firstpart}s/.*/&/p" inputfile>outfile
sed -n "${currentline}s/.*/&/p" inputfile>>outfile
sed -n "$secondpart,\$s/.*/&/p" inputfile>>outfile
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:33 PM.


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

Content Relevant URLs by vBSEO 3.2.0