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

10,884

Solution 1

With sed:

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

If you want to remove spaces after ::

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

Solution 2

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

Solution 3

awk -F ": " '$1 ~ /Product Name$/ {print $2}' dmidecode.txt
Share:
10,884
Ryan
Author by

Ryan

Updated on September 07, 2022

Comments

  • Ryan
    Ryan over 1 year

    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      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 :*\).*/\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.

  • Shizzmo
    Shizzmo over 12 years
    you don't need cat. just grep "Product Name" input.txt | cust -f2 -d:
  • bpgergo
    bpgergo over 12 years
    @Shin, thx, you're right, even more simpler without cat. I just get used to the way I use these tools: I always have them read from the standard input rather then giving them an actual file as an argument.
  • Ryan
    Ryan over 12 years
    Wow, I was making this way too difficult! Thank you guys, now I just need to put the output into a variable and DONE!
  • Ryan
    Ryan over 12 years
    thanks jfgagne! your solution also worked. Btw, how do you figure out what to put between the curly braces? {s/.*: *//p} ??? what is THAT? I cant seem to find a good resource on that anywhere...
  • David W.
    David W. over 12 years
    What's the advantage of this over sed? With sed, you have a single process that does everything in one shot. Here, you are using three separate processes piping one to the other: One to cat out the file, one to find the lines, and one to cut out the field you want. Even worse, it's hard to test for errors in the first two commands. What if grep can't find the string?
  • bpgergo
    bpgergo over 12 years
    @David, for me, grep+cut has no advantage over sed. On the other hand, I thought it'd be easier to understand for OP if confused with sed. See OP's comment on jfgagne's sed answer. 'what is THAT?' Clearly, OP does not understand what is happening there, therefore OP might face further difficulties when maintaining this script.
  • Ryan
    Ryan over 12 years
    What is "OP"? And you're right, I dont fully understand what is happening, any suggestions on helpful resources?
  • jfg956
    jfg956 over 12 years
    @Ryan: s is the substitute command in sed, the next char (/) is the delimiter, . means any character, * means 0 or more occurrence of last character, : means itself, and p is the print command in sed if a substitution was made; so s/.*://p means replace everything before a : and all the following spaces by nothing, and then print the result if a substitution was made. Note that sed normal output was disabled using the -nargument. We could also use s/.*://;p to print the result even if no substitution were made.
  • jfg956
    jfg956 over 12 years
    @Ryan: I suggest this reading: Sed - An Introduction and Tutorial by Bruce Barnett.