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

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

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

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

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
58.228.111.111:/my/file/system /my/file/system nfs rw,soft,bg,intr,vers=3 0 0

to this

58.228.111.111:/BAR/my/file/system /my/file/system nfs rw,soft,bg,intr,vers=3 0 0
  1. I don't understand how this is working
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.

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

close, but no cigar

If your sed supports extended regex (-r) then this should do it:

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.

getting closer. learned how to combine regexp

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

Try this (sorry about previous answer missed the begins with "58" bit):

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

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

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

No the devil is in the detail, thanks. Funny, sometimes I read what I expect to see.