SED 替换引号内的文本 [英] SED to replace text inside quotes

查看:44
本文介绍了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

这篇关于SED 替换引号内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆