Semion Polonsky
Semion Polonsky

Reputation: 107

sed regex the last match

I want to change in file in each line the last match to the XXXX Given the file format:

"192.xx2" "someting" "another2321SD" 

I want to have:

"192.xx2" "someting" "XXXX" 

only using sed My solution:

sed -r 's/"(?:.(?!".+"))+$/XXXX/' file

Upvotes: 2

Views: 255

Answers (2)

choroba
choroba

Reputation: 241808

Remember what was there before the last pair of double quotes in a capture.

sed -r 's/^(.*)"[^"]*"$/\1"XXXX"/'

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

Please bear in mind that

To mask all content in between the last pair of double quotation marks, you can use

sed -r 's/(.*)".*"/\1"XXXX"/' file
sed 's/\(.*\)".*"/\1"XXXX"/' file

See the sed demo:

s='"192.xx2" "someting" "another2321SD"'
sed -r 's/(.*)".*"/\1"XXXX"/' <<< "$s"
# => "192.xx2" "someting" "XXXX"

Upvotes: 1

Related Questions