Bash – Text file look-up by column

awkbashfiltersedshell-script

I have a file in this format:

[#]   OWNER_NAME     NAME                       SIZE
[6]   Robottinosino  Software                   200
[42]  Robottinosino  Ideas worth zero           188
[12]  Robottinosino  Ideas worth zero or more   111
[13]  I am Batman    Hardware                   180
[25]  Robottinosino  Profile Pictures           170

and I would like to be able to do the following using command line tools:

my_command "Ideas worth zero"

and get this result:

42

and not risk getting this result:

12

I have thought of using grep to identify the line, awk to get the 1st field but I am not sure how to reliably and efficiently match on the whole 'NAME' field short of counting at which column the text 'OWNER_NAME' and 'SIZE' appear in the header and get everything in-between with some whitespace trimming.

Notice 'OWNER_NAME' could be more than one word: e.g. 'OWNER_NAME' = "I am Batman".

Any ideas with accompanying implementation?

What I have to go by here, is just the old family of cat, head, tail, awk, sed, grep, cut, etc.

Best Answer

OK, if the length of the columns is not known, I'd switch to a more powerful language than bash:

#!/usr/bin/perl
use warnings;
use strict;

my $string = shift;
open my $FH, '<', '1.txt' or die $!;
my $first_line = <$FH>;
my ($before, $name) = $first_line =~ /(.* )(NAME *)/;
my $column = length $before;
$string .= ' ' x (length($name) - length $string);     # adjust the length of $string
while (<$FH>) {
    if ($column == index $_, $string, $column) {
        /^\[([0-9]+)\]/ and print "$1\n";
    }
}
Related Question