Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed regex not working 1

Status
Not open for further replies.

grazinggoat2

Systems Engineer
Jun 25, 2019
6
US
hello
I have an file that has data like this:
prod:admin:batch:weekend:[highlight #FCE94F]endday_load_billing[[/highlight]:SERVER:server1
prod:admin:batch:daily: [highlight #FCE94F]sunday_load_pricing_job1[/highlight]:SERVER:server1
prod:admin:batch:daily:[highlight #FCE94F]sunday_load_pricing_job2[/highlight]: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
 
Hi

I would use an address to split the regular expression in 2 less complex ones :
Code:
[gray]               address                  command
     ( in lines matching this )       ( do this )
     ╭────────────┴───────────╮ ╭──────────┴────────╮[/gray]
sed '/^prod:admin:batch:daily:/ s/:server1$/:server2/' /tmp/output.txt


Feherke.
feherke.github.io
 
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!
 
Hi

grazinggoat2 said:
why would sed's[COLOR=red yellow]/[/color]<address>[COLOR=red yellow]/[/color]cmd[COLOR=red yellow]/[/color]cmdsubstitute[COLOR=red yellow]/[/color]' 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 ( [tt]/[/tt] ).

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 :
[ul]
[li]
man sed said:
[pre] /regexp/
Match lines matching the regular expression regexp. Matching is
performed on the current pattern space, which can be modified
with commands such as ‘‘s///’’.[/pre]
[/li]
[li]
man sed said:
[pre] s/regexp/replacement/
Attempt to match regexp against the pattern space. If success‐
ful, replace that portion matched with replacement.[/pre]
[/li]
[/ul]

As they are separate things, in different situations they could be paired with other addresses/commands. For example :
[ul]
[li]Delete all daily jobs :
Code:
/^prod:admin:batch:daily:/ d
[/li]
[li]Do the replacement in lines 2 and 3 :
Code:
2,3 s/:server1$/:server2/
[/li]
[/ul]

By the way, in case of [tt]s[/tt] command the slashes are just the traditional delimiters but you can use convenient character. Next frequently used one is usually the exclamation mark ( [tt]![/tt] ), especially when working with file path. But can be almost anything, including letter s too, as long as you escape the literal ones :
Code:
 [green]s command's delimiters[/green]
 [green]╭──────────┼─────────╮[/green]
 [green]▼          ▼         ▼[/green]
[b]s[/b][green]s[/green][u]:[red]\[/red][blue]s[/blue]erver1$[/u][green]s[/green][u]:[red]\[/red][blue]s[/blue]erver2[/u][green]s[/green]
   [red]▲[/red][blue]▲[/blue]         [red]▲[/red][blue]▲[/blue]
   [red]│[/red][blue]╰─────┬───[/blue][red]│[/red][blue]╯[/blue]
   [red]│[/red] [blue]literal letter s[/blue]
   [red]╰─────┬────╯[/red]
      [red]escaped[/red]

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.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top