在 unix 中提取特定出现的引号之间的字符串 [英] Extract the string between Quotes of particular occurrence in unix

查看:10
本文介绍了在 unix 中提取特定出现的引号之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入文件

..
set name "old name"; # comment "should be updated"
..

输出文件

..
set name "new name" ; #comment "should be updated"
..

当我尝试使用 grep -i 'name' inputfile | grep 引号之间的内容时 |grep -P ".+{"} 它在第一个和最后一个"之间的grepping内容

when i tried to grep the content between quotes with grep -i 'name' inputfile | grep -P ".+{"} its grepping content between first " and last "

i.e old name"; # comment "应该更新

任何使用grepsedawk 来实现的想法!

any idea to accomplish that using grep, sed or awk!

推荐答案

目前还不清楚您到底要做什么.一方面,您的 grep 命令包括一个花括号,而输入没有.此外,您似乎想根据输入和输出的比较进行替换.

It's unclear exactly what you're trying to do. For one thing, your grep command includes a curly brace and the input doesn't. Also, it appears that you want to make a substitution based on a comparison of your input and output.

然而,从字面上看你的问题,这里是你如何grep引号之间的字符串.您可以使用非贪婪匹配:

However, taking your question literally, here's how you can grep the strings between the quotes. You can use non-greedy matching:

grep -Po '".*?"'

示例:

$ echo 'set name "username"; # comment "should be updated"' | grep -Po '".*?"'
"username"
"should be updated"

为了替换一个值,你可以使用 sed.您不会使用 grep.

In order to substitute a value, you can use sed. You would not use grep.

sed 's/"[^"]*"/"new name"/'

示例:

$ echo 'set name "old name"; # comment "should be updated"' | sed 's/"[^"]*"/"new name"/'
set name "new name"; # comment "should be updated"

这篇关于在 unix 中提取特定出现的引号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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