toop
toop

Reputation: 11304

sed regex replace on solaris

I have a file dummy.txt containing this:

"my_server"\1\"n9j7gd8kl4"
"widget"\1\"vnhck67hn"
"other_server"\1\"tbone"
"blah"\1\"n9j7gd8kl4"
"server_new"\1\"g54"
"genserver"\1\"vf45s"
"prd+other_server"\1\"f"\"jh34t"
"test_blah"\1\"tbone"

I need to change it to this with a generic-like one-liner in Solaris (can anyone please help?):

"my_server"\1\"tbone"
"widget"\1\"vnhck67hn"
"other_server"\1\"tbone"
"blah"\1\"n9j7gd8kl4"
"server_new"\1\"tbone"
"genserver"\1\"tbone"
"prd+other_server"\1\"f"\"jh34t"
"test_blah"\1\"tbone"

ie. For every line that has the string 'server' within the first double quotes and the line is in the format "string1"\1\"string2" then change the value of string2 to 'tbone'

Upvotes: 0

Views: 2216

Answers (2)

Perleone
Perleone

Reputation: 4038

cat dummy.txt | perl -pe 's{ ^ (" [^"\\]* server [^"\\]* " \\1 \\") [^"\\]+ " $}{${1}tbone"}xms;'

The Perl version is slightly more readable.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 755010

sed 's/^\("[^"]*server[^"]*"\\1\\\)".*"$/\1"tbone"/'

This allows 'server' to appear anywhere within the first string, not just at the end as in all the examples. If you only want it at the end, omit the second [^"]*.

Oh, and to deal with the 'no double quotes or backslashes' requirement:

sed 's/^\("[^\\"]*server[^\\"]*"\\1\\\)"[^\\"]*"$/\1"tbone"/'

The difference is the backslashes in the negated character classes.

Upvotes: 3

Related Questions