bash 解析带有前缀“产品名称:”的特定字符串的文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7068292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:36:00  来源:igfitidea点击:

Parse a text file for certain string with prefix "Product Name:"

bashsedgrep

提问by Ryan

Hey guys I've got a text file that a script creates (specifically dmidecode > dmidecode.txt) and I want to be able to grab the contents of "Product Name:" so in this case "HP t5740e Thin Client" but it will be slightly different for other machine types. I could just use sedto count to line 44 and then slice it up until I get what I want but I'd like for it to be more dynamic than that.

嘿伙计们,我有一个脚本创建的文本文件(特别是dmidecode > dmidecode.txt),我希望能够获取“产品名称:”的内容,所以在这种情况下是“HP t5740e 瘦客户端”,但对于其他人来说会略有不同机器类型。我可以使用sed数到第 44 行,然后将其切片,直到得到我想要的东西,但我希望它比这更动态。

Text file:

文本文件:

41  Handle 0x0001, DMI type 1, 27 bytes
42  System Information
43      Manufacturer: Hewlett-Packard
44      Product Name: HP t5740e Thin Client
45      Version:   
46      Serial Number: CNW1160BZ7
47      UUID: A0B86400-6BBD-11E0-8325-92EEE331A344
48      Wake-up Type: Power Switch
49      SKU Number: XL424AA#ABA
50      Family: 103C_53302C

Code I have that doesn't seem to work:

我的代码似乎不起作用:

sed -c -i "s/\($TARGET_KEY *Product Name :*\).*/$REPLACEMENT_VALUE/" dmidecode.txt

I get the feeling my regular expressions is way off (probably because the initial examples I looked at tainted my "vision")

我觉得我的正则表达式离我很远(可能是因为我看到的最初的例子玷污了我的“愿景”)

Any help is greatly appreciated! Also, anyone know of any good regular expression references I can check out?

任何帮助是极大的赞赏!另外,有人知道我可以查看的任何好的正则表达式参考吗?

UPDATE: Ok I was able to spend a little more time on this, found some better examples and got this out of my research:

更新:好的,我可以花更多的时间在这上面,找到了一些更好的例子,并从我的研究中得到了这个:

grep -e "Product Name: HP" -e "Product Name: hp" dmidecode.txt | awk '{print}'

When I add '{print $NF}'it prints just the last word, is there a way to modify print to include everything after the search string instead of the whole line itself?

当我添加'{print $NF}'它时只打印最后一个单词,有没有办法修改打印以包含搜索字符串之后的所有内容而不是整行本身?

Also, I should have noted this from the beginning, but I need the output to go into a variable.

另外,我应该从一开始就注意到这一点,但是我需要将输出放入一个变量中。

回答by bpgergo

you won't even need sed for that

你甚至不需要 sed

grep "Product Name" input.txt | cut -f2 -d ":"

explanation

解释

grep "Product Name"give me only the lines containing "Product Name"

grep "Product Name"只给我包含“产品名称”的行

cut -f2 -d ":"split those lines using ":" as delimiter and the return second field

cut -f2 -d ":"使用“:”作为分隔符并返回第二个字段分割这些行

回答by jfg956

With sed:

使用 sed:

sed -n -e '/Product Name/{s/.*://p}'

If you want to remove spaces after ::

如果您想在 之后删除空格:

sed -n -e '/Product Name/{s/.*: *//p}'

回答by glenn Hymanman

awk -F ": " ' ~ /Product Name$/ {print }' dmidecode.txt