简体   繁体   English

SED替换引号内的文本

[英]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 仅触摸以%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? sed实用程序的正确语法是什么?

awk can do the job easily: awk可以轻松完成这项工作:

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}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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