Murat İNAN
Murat İNAN

Reputation: 13

how can i get certain columns and certain rows from file with egrep and awk

This is my data and file name : example.txt

id   name    lastname point
1234;emanuel;emenike;2855
1357;christian;baroni;398789
1390;alex;souza;23143
8766;moussa;sow;5443

I want to see who has this id(1234, 1390) columnname and point like that

emanuel 2855
alex    23143

How can i do this in linux command line with awk and egrep

Upvotes: 1

Views: 109

Answers (5)

Bernie Reiter
Bernie Reiter

Reputation: 116

Use the GNU version of awk (= gawk) in a two step approach to make your solution very flexible:

Step 1:
Parse your data file (e.g., example.txt) to generate a gawk lookup-function (here called "function_library.awk"):

$ /PATH/TO/generate_awk_function.sh /PATH/TO/example.txt

"generate_awk_function.sh" is just an gawk script for printing:

#! /bin/bash -

gawk 'BEGIN {
        FS=";"
        OFS="\t"
        print "#### gawk function library \"function_library.awk\""
        print "function lookup_value(key,      value_for_key) {"
}

{
        if (NR > 1 ) print "\tvalue_for_key["$1"] = \"" $2 OFS $4 "\""
}

END {
        print "    print value_for_key[key]"
        print "}"
}' $1 > function_library.awk

You have generated this lookup function:

$ cat function_library.awk
#### gawk function library "function_library.awk"
function lookup_value(key,      value_for_key) {
        value_for_key[1234] = "emanuel  2855"
        value_for_key[1357] = "christian        398789"
        value_for_key[1390] = "alex     23143"
        value_for_key[8766] = "moussa   5443"
    print value_for_key[key]
}

Adapt "generate_awk_function.sh" for your needs:
a) FS=";" is setting the field separator in your input file (here a semicolon)
b) OFS="\t" is setting the output field separator (here a TAB)

You only have to generate this gawk "lookup-function" anew when your "example.txt" has changed.

Step 2:
Read your IDs to look up your results:

$ cat id.txt 
1234
1390

$ gawk -i function_library.awk '{lookup_value($1)}' id.txt 
emanuel 2855
alex    23143

You can also use this approach in a pipe like this:

$ cat id.txt | gawk -i function_library.awk '{lookup_value($1)}' 

or like this:

$ echo 1234 | gawk -i function_library.awk '{lookup_value($1)}' 

You can adapt this approach if your lookup string (1234) or file (id.txt) is containing some additional unwanted data ("noise") by using simple awk means:

a) Here, too, you can define a field separator, e.g., by setting it to a colon (:)

$ gawk -F":" -i function_library.awk '{lookup_value($5)}' id.txt

b) You can use the nth field of your lookup string, e.g., setting it from the 1st field to the 5th field just by changing the lookup_value from $1 to $5:

$ gawk -i function_library.awk '{lookup_value($5)}' id.txt

Please be aware that the '-i' command-line option is only supported by the GNU version of awk (= gawk).

HTH

bernie

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Through awk,

awk -F';' '$1~/^1234$/ || $1~/^1390$/ {print $2,$4}' file

Example:

$ cat ccc
id   name    lastname point
1234;emanuel;emenike;2855
1357;christian;baroni;398789
1390;alex;souza;23143
8766;moussa;sow;5443
$ awk -F';' '$1~/^1234$/ || $1~/^1390$/ {print $2,$4}' ccc
emanuel 2855
alex 23143

Upvotes: 1

Jotne
Jotne

Reputation: 41446

Some variation awk

awk -F\; '$1~/^(1234|1390)$/ {print $2,$4}' file
emanuel 2855
alex 23143

Upvotes: 3

choroba
choroba

Reputation: 241768

Using grep and cut:

grep '^\(1234\|1390\);' input | cut -d\; --output-delimiter=' ' -f2,4

Upvotes: 4

dogbane
dogbane

Reputation: 274532

You can try this:

awk -F\; '$1=="1234" || $1=="1390" {print $2,$4}' file

Upvotes: 4

Related Questions