SED 替换引号内的文本
问题描述:
假设我有一个文本文件,其中一些行是这种格式:
Assuming I have a text file where some lines are of this format:
%attr(750,user1,group1) "/mydrive/mypath1/mypath2\winpath1\winpath2\file.xml"
我想要实现的是:
- 仅触摸以 %attr 开头的那些行
- 在每一行中找到最后一个.*"(包括引号)的场合
- 在最后一次将所有
\
替换为/
- 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/
sed 实用程序的正确语法是什么?
What is the proper syntax for sed utility?
答
awk 可以轻松完成这项工作:
awk can do the job easily:
awk -F '"' '/^%attr/ {gsub(/\\/, "/", $(NF-1))} 1' OFS='"' file
更改原始文件:
awk -F '"' '/^%attr/ {gsub(/\\/, "/", $(NF-1))} 1' OFS='"' file > _tmp && mv _tmp file