开发者

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

开发者 https://www.devze.com 2023-03-28 01:05 出处:网络
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

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 sed to 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.

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      Seria开发者_StackOverflow社区l 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 :*\).*/\1$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?

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


you won't even need sed for that

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

explanation

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

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


With sed:

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

If you want to remove spaces after ::

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


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

精彩评论

暂无评论...
验证码 换一张
取 消