SED to replace text inside quotes

2020-04-17 05:58发布

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?

标签: regex sed
2条回答
叛逆
2楼-- · 2020-04-17 06:25

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
查看更多
够拽才男人
3楼-- · 2020-04-17 06:34

you can try this sed '/\%attr/{s/\\/\//g}'

查看更多
登录 后发表回答