Register
It is currently Thu Dec 18, 2014 6:42 am

Sed command to replace 3rd and 4th instance in a file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Fri Nov 11, 2011 2:55 pm   

Joined: Tue Jun 28, 2011 4:38 pm
Posts: 7
Hey all you bashing gurus..

I have a files that have 6 instances of the string

&> /dev/null

I need to remove the 3rd and 4th instance in the file (not on the line)

I tried to use sed to do this:

Code:
sed -i 's/\&> \/dev\/null//3' go_ams_pop


but that did not work..

Any ideas?

thanks

~Dynacade


Top
 Profile  
 PostPosted: Sat Nov 12, 2011 4:36 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Code:
grep -n ^ $file |
    grep '&>/dev/null' |
    cut -f1 -d: |
    head -n4 | tail -n2 |
    sed 's,$,s=&>/dev/null==,' |
    sed -f- $file

How does it work? It numbers lines in the file, selects only those lines containing the string, takes their line numbers, selects the 3rd and 4th, and then does the replacement only at the given lines.


Top
 Profile  
 PostPosted: Sun Nov 13, 2011 11:17 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Using a counter in awk(1).

Code:
awk '/&> \/dev\/null/ { o++; if (o ~ /(3|4)/) sub(/&> \/dev\/null/, _) } 1' file > file_tmp
mv file_tmp file


Using ed(1) (so no temporary file creation needed, but less understandable and modifiable):
Code:
ed -s file <<< $'/&> \/dev\/null/\n//\n//s,&> /dev/null,,\n//s\nw'


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 10:31 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
jsz wrote:
Using ed(1) (so no temporary file creation needed, but less understandable and modifiable):
Code:
ed -s file <<< $'/&> \/dev\/null/\n//\n//s,&> /dev/null,,\n//s\nw'


Very nice :D


Top
 Profile WWW  
 PostPosted: Tue Nov 15, 2011 10:58 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
crouse wrote:
jsz wrote:
Using ed(1) (so no temporary file creation needed, but less understandable and modifiable):
Code:
ed -s file <<< $'/&> \/dev\/null/\n//\n//s,&> /dev/null,,\n//s\nw'



Very nice :D


Thank you. : -)


Top
 Profile  
 PostPosted: Sun Apr 01, 2012 6:44 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Reading this and thinking differently.

Code:
ed -s file <<< $'/&> \/dev\/null/\n\n\ns///\n//s\nw'


It's actually shorter.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP