简体   繁体   中英

SED to replace text inside quotes

Assuming I have a text file where some lines are of this format:

%attr(750,user1,group1)  "/mydrive/mypath1/mypath2\winpath1\winpath2\file.xml"

What I wanna achieve is:

  • touch only those lines which start with %attr
  • on each of such lines find the last occasion of ".*" (including quotes)
  • inside that last occasion replace all \\ to /

What is the proper syntax for sed utility?

awk can do the job easily:

awk -F '"' '/^%attr/ {gsub(/\\/, "/", $(NF-1))} 1' OFS='"' file

To change the original file:

awk -F '"' '/^%attr/ {gsub(/\\/, "/", $(NF-1))} 1' OFS='"' file > _tmp && mv _tmp file

您可以尝试使用此sed '/\\%attr/{s/\\\\/\\//g}'

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