If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Help needed in a shell script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-11-07, 01:47
ranga2727 ranga2727 is offline
Registered User
 
Join Date: Sep 2007
Posts: 11
Help needed in a shell script

Hi,

I am try to remove certain lines from a file

here is what i need :

File1 .txt

aaa
bbb
bbb

wewe
wreq
1234
11111
1111
13324
2332434
1343434

i am searching for a string 1234 once that string is found i need to remove 1234 and the lines which are above (2 lines and 5 lines down ) these are constant for my work

So the output file should be only like this ::
=====================================
aaa
bbb
bbb
Reply With Quote
  #2 (permalink)  
Old 09-11-07, 05:39
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
If you have GNU grep:

Code:
grep -v "$(grep -B2 -A5 1234 yourfile)" yourfile
Reply With Quote
  #3 (permalink)  
Old 09-11-07, 09:21
ranga2727 ranga2727 is offline
Registered User
 
Join Date: Sep 2007
Posts: 11
i donot have a gnu grep any other option ??

Thanks
Reply With Quote
  #4 (permalink)  
Old 09-11-07, 10:19
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
If your file is not big:

Code:
awk '{x[NR]=$0}
	/1234/{y[NR]}END{
		for(j in y)
			for(i=j-2;i<j+6;i++)
				delete x[i]
		for(i=1;i<=NR;i++)
			if(i in x)
	print x[i]
}' file
If your awk doesn't support the delete statement:

Code:
awk '{x[NR]=$0}
	/1234/{y[NR]}END{
		for(j in y)
			for(i=j-2;i<j+6;i++)
				x[i]="not_this_one"
		for(i=1;i<=NR;i++)
			if(x[i]!="not_this_one")
	print x[i]
}' file
Reply With Quote
  #5 (permalink)  
Old 09-15-07, 12:28
ranga2727 ranga2727 is offline
Registered User
 
Join Date: Sep 2007
Posts: 11
Thanks It worked .. Thanks a lot !!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On