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

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

问题描述

输入文件

  .. 
设置名称old name; #comment应该更新
..

输出文件

  .. 
设置名称new name; #comment应该更新
..

当我尝试grep内容引用 grep -i'name'输入文件| grep -P \。+ {\}} 它在第一个和最后一个之间的grepping内容



old name;#comment应该更新



使用 grep sed awk

解决方案

目前还不清楚你想要做什么。一方面,您的 grep 命令包含一个大括号,而输入不包含。此外,您似乎希望根据您的输入和输出进行比较来进行替换。



然而,从字面上理解您的问题,您可以如何 grep 引号之间的字符串。您可以使用非贪婪匹配:

  grep -Po'。*?'

$ $ p $ $ $ $ $ $ echo $'$ $ $ $ $ $ $ $ $'设置名称用户名; #评论应该更新'| grep -Po'。*'''
username
should be updated

编辑

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

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

示例:

  $ echo'set nameold name;#commentshould be updated'| sed's /[^] */new name /'
设定名称新名称; #comment应该更新


Input file

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

Output file

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

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 "should be updated

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

解决方案

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.

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

grep -Po '".*?"'

Example:

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

Edit:

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

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

Example:

$ 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天全站免登陆