3

A continuation of this question: parse first column of command output, get corresponding second column value Say I have a command that outputs a string formatted as a table, as shown below. What if the "pattern" I am looking for includes spaces? For example, if the table is:

First Characteristic:     b
Second Characteristic:    89.4
Version:                  58.93
Name of Device:           myDevice
Name of Device Load:      myDevice-load-123abc

What if I want to get the value next to "Name of Device Load" in the table above?

To clarify, I know the value I am looking for is next to "Name of Device Load." I do not know that it is in the 5th row of the output, and I do not know anything about what that value would look like (So I can't try pattern matching with something like "-load-", for example).

  • Is a perl solution acceptable? – Sobrique Jul 8 '15 at 20:34
  • Some of the answers that been posted assume that the second column value cannot include colons or spaces.  It would be helpful if you would clarify whether that is true. – Scott Jul 9 '15 at 1:55
2

What about this :

grep "Name of Device Load:" your_file.txt| cut -d ":" -f 2 | tr -d " "

This only keeps the line you are interested in, separates it into two fields (works only if : is only present once per line), then remove spaces.

2

I'd use perl for this.

Something like this:

#!/usr/bin/env perl

use strict;
use warnings;

local $/;
my %lookup = ( <> =~ m/^(.*): (\S+)$/gm );
print $lookup{'Name of Device Load'},"\n";

We 'read in' the data from either STDIN or filenames: myscript.pl filename (That's what <> does, it's a bit magic).

We 'parse' it using regular expression capture groups the above, and because we capture two elements at time (and repeatedly across multiple lines thanks to the gm flags) we create a hash table of key-value pairs.

Then extract the value associated with the named key Name of Device Load and print it, so the ordering is irrelevant.

1

You need to define how the fields are delimited one way or another. In your case, it seems that each line consists of a tag ending with a colon, then some whitespace, then a value. With awk, you can set the record delimiter to :, or (with most awk versions) to : and following whitespace.

awk -v RS=':[ \t]*' '$1 == "Name of Device Load" {print $2}'

Note that $2 only matches up to the second colon on the line. If you want to print the rest of the line, you can strip off the tag:

awk -v RS=':[ \t]*' '$1 == "Name of Device Load" {sub(/^[^:]*:[ \t]*/); print $0}'

If all you want to do is extract data from lines matching a specific pattern, then using sed does the job more directly. Pass the -n option to turn off automatic printing, and print only the lines for which stripping off the desired tag succeeds.

sed -n 's/^Name of Device Load: *//p'
1

I would use the awk -F":" for it. So if you wanted the second column, you could do

awk -F":" 'FNR == 5 {print $2}'

This is a makeshift solution but would work in this case.

If the pattern you know and want is "Name of Device Load", you could do

cat file.txt | grep "Name of Device Load" | awk -F":" '{print $2}'

EDIT: Added the option for the 5th line and second line and search for pattern. Basically followed this thread

https://stackoverflow.com/questions/1506521/select-row-and-element-in-awk

  • The question says 'I do not know that [the value I am looking for] is in the 5th row' and 'I know the value I am looking for is next to "Name of Device Load".' – Scott Jul 9 '15 at 1:47
-2

through below script we can get second value

awk -F: /Name of Device Load:/ { print $2} file1

  • 1
    A correct version of that answer (with proper quoting) has already been given. – Stéphane Chazelas Dec 14 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.