Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

sed with multiple regexp

Shell Programming and Scripting


Tags
ip, regex, sed

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-07-2012
snoman1 snoman1 is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 22 December 2014, 5:34 PM EST
Posts: 21
Thanks: 9
Thanked 1 Time in 1 Post
sed with multiple regexp

Dealing with Linux servers

script would be in korn or bash shell syntax

file is /etc/fstab

I want to insert something if regex is matched to all matched lines in the /etc/fstab file and print out entire /etc/fstab file with the changes

example

Code:
58.228.111.111:/my/file/system /my/file/system nfs rw,soft,bg,intr,vers=3 0 0

if uncommented line starts with 58. "^58\."
and line doesn't contain FOO
and line doesn't contain BOO
and if the line meets these requirements, insert the word BAR in a certain location

finished line would look like this

Code:
58.228.111.111:/BAR/my/file/system /my/file/system nfs rw,soft,bg,intr,vers=3 0 0

one line that comes close is this

Code:
sed -e '/THIS/!s/\:\//\:\/BAR\//g'

This of course is my first attempt which doesn't work but it does kinda show
you what I am looking for

Code:
grep "^58\." /etc/fstab |egrep -v "FOO|BOO" |sed -e "s/\:\//\:\/BAR\//g"

so my question is 2 part

1. how can I change a line like this

Code:
58.228.111.111:/my/file/system /my/file/system nfs rw,soft,bg,intr,vers=3 0 0

to this

Code:
58.228.111.111:/BAR/my/file/system /my/file/system nfs rw,soft,bg,intr,vers=3 0 0

2. I don't understand how this is working

Code:
sed -e '/THIS/!s/\:\//\:\/BAR\//g'

I understand that "!s" section is saying don't look at lines with THIS in it

in general this is using a SED command, using a regex to throw out 2 seperate expressions, and include one regex.


Code:
sed '/^58\./,/FOO/!s//BAR/g' /etc/fstab

close, but no cigar

Last edited by Franklin52; 03-08-2012 at 02:37 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old Unix and Linux 03-07-2012
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 20 April 2015, 5:10 PM EDT
Posts: 3,133
Thanks: 121
Thanked 1,031 Times in 965 Posts
If your sed supports extended regex (-r) then this should do it:


Code:
sed -r '/(FOO|BAR)/!s_:/_:/BAR/_' /etc/fstab


Note: this will only print what would be done to edit in-place use -i or --in-place option.

Last edited by Chubler_XL; 03-07-2012 at 06:50 PM..
Sponsored Links
    #3  
Old Unix and Linux 03-07-2012
snoman1 snoman1 is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 22 December 2014, 5:34 PM EST
Posts: 21
Thanks: 9
Thanked 1 Time in 1 Post
getting closer. learned how to combine regexp

Code:
cat /etc/fstab |sed -e '/FOO\|BOO/!s/\:\//\:\/BAR\//g'


Last edited by Franklin52; 03-08-2012 at 02:37 AM.. Reason: Please use code tags for code and data samples, thank you
    #4  
Old Unix and Linux 03-07-2012
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 20 April 2015, 5:10 PM EDT
Posts: 3,133
Thanks: 121
Thanked 1,031 Times in 965 Posts
Try this (sorry about previous answer missed the begins with "58" bit):


Code:
 sed -r '/^58([^(FOO|BAR)]*)$/s_:/_:/BAR/_' /etc/fstab

Sponsored Links
    #5  
Old Unix and Linux 03-07-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 507 Times in 486 Posts
Quote:
Originally Posted by Chubler_XL View Post
Try this (sorry about previous answer missed the begins with "58" bit):


Code:
 sed -r '/^58([^(FOO|BAR)]*)$/s_:/_:/BAR/_' /etc/fstab

Not to nit pick too much, the first BAR should be BOO

Code:
 sed -r '/^58([^(FOO|BOO)]*)$/s_:/_:/BAR/_' /etc/fstab

The Following User Says Thank You to agama For This Useful Post:
Chubler_XL (03-07-2012)
Sponsored Links
    #6  
Old Unix and Linux 03-07-2012
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 20 April 2015, 5:10 PM EDT
Posts: 3,133
Thanks: 121
Thanked 1,031 Times in 965 Posts
No the devil is in the detail, thanks. Funny, sometimes I read what I expect to see.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed before and after regexp soly UNIX for Dummies Questions & Answers 2 03-02-2010 03:56 AM
sed regexp King Nothing Shell Programming and Scripting 4 12-18-2009 06:34 AM
print the line immediately after a regexp; but regexp is a sentence ownins UNIX for Dummies Questions & Answers 3 11-16-2008 01:30 AM
regexp help TinCanFury Shell Programming and Scripting 0 04-06-2008 06:21 PM
regexp penfold UNIX for Advanced & Expert Users 1 06-28-2005 07:45 AM



All times are GMT -4. The time now is 02:30 AM.