The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 10:04 AM
SED command ---------help needed veerapureddy Shell Programming and Scripting 4 03-17-2008 05:12 PM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
Help with tail /grep needed Felix2511 UNIX for Dummies Questions & Answers 4 05-17-2007 03:22 PM
help needed for sed command manualvin UNIX for Dummies Questions & Answers 2 06-24-2004 11:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-10-2007
ranga27 ranga27 is offline
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..!!
  #2 (permalink)  
Old 09-10-2007
ranga27 ranga27 is offline
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..!!
  #3 (permalink)  
Old 09-10-2007
varungupta varungupta is offline
Registered User
  
 

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

Hope this is what you want !!

Thanks !
  #4 (permalink)  
Old 09-10-2007
fazliturk fazliturk is offline
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
  #5 (permalink)  
Old 09-10-2007
fazliturk fazliturk is offline
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
  #6 (permalink)  
Old 09-10-2007
ranga27 ranga27 is offline
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0