Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

sed regex not working

sed regex not working

sed regex not working

I have an file that has data like this:
prod:admin:batch:daily: sunday_load_pricing_job1:SERVER:server1

I want to update the server names so since each job is name is different the global using vi editor would change all the server names
if i key'd on the server name itself. Plus i don't want to update the weekend job

I've tried the following and several other attempts but none work:
sed 's/prod:admin:batch:daily:[-a-zA-Z0-9_\#\@\.]+:SERVER:server1/SERVER:server1/SERVER:server2/g' /tmp/output.txt
sed -e -r 's/prod:admin:batch:daily:[-a-zA-Z0-9_\#\@\.]+:SERVER:server1/SERVER:server1/SERVER:server2/g' /tmp/output.txt

RE: sed regex not working


I would use an address to split the regular expression in 2 less complex ones :


               address                  command
     ( in lines matching this )       ( do this )
     ╭────────────┴───────────╮ ╭──────────┴────────╮
sed '/^prod:admin:batch:daily:/ s/:server1$/:server2/' /tmp/output.txt 


RE: sed regex not working

This worked. I wasn't aware that the command could follow the address that i wanted to match. why would sed's/<address>/cmd/cmdsubstitute/' now work seems its worked for me in the past which is why i was confused on it not working now?

Thanks for taking the time to respond earlier. Appreciate you!

RE: sed regex not working


Quote (grazinggoat2)

why would sed's/<address>/cmd/cmdsubstitute/' now work seems its worked for me in the past which is why i was confused on it not working now?
Pretty sure it never worked exactly like that, as there is syntax error due to too many slashes ( / ).

Do not let yourself confused by the fact that both the s command and the regular expression address are using slashes as delimiters. They are completely separate things :
  • Quote (man sed)

                  Match lines matching the regular expression regexp.  Matching is
                  performed  on  the  current pattern space, which can be modified
                  with commands such as ‘‘s///’’. 
  • Quote (man sed)

                  Attempt  to match regexp against the pattern space.  If success‐
                  ful, replace that portion matched  with  replacement. 
As they are separate things, in different situations they could be paired with other addresses/commands. For example :
  • Delete all daily jobs :

    CODE --> sed

    /^prod:admin:batch:daily:/ d 
  • Do the replacement in lines 2 and 3 :

    CODE --> sed

    2,3 s/:server1$/:server2/ 
By the way, in case of s command the slashes are just the traditional delimiters but you can use convenient character. Next frequently used one is usually the exclamation mark ( ! ), especially when working with file path. But can be almost anything, including letter s too, as long as you escape the literal ones :

CODE --> sed

s command's delimiters
 ▼          ▼         ▼
    literal letter s

Which is not valid for the address, as there you can only use slashes.

Note that this may depend on the sed implementation you use. Certainly works like this in GNU sed.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close