简体   繁体   中英

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

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

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM