Pradyumna Sagar
Pradyumna Sagar

Reputation: 403

how to print first n column and a column which matches the pattern?

I have a tab delimited file with n rows and m columns, I want to print the first three column and search for a pattern and print that column if present. I tried to search and print in sed but unable to do it to print first 3 column and then search for the pattern.

example I have file like

col1    col2    col3    col4    col5    col6
test    23      2323    32      635     36354
test2   354     35b     345     345     555
test4   486     g4      435     0.43    34
test5   0.6     35      0.34    0.234   34563

output I want is (if pattern I search is 'col6' for example)

col1    col2    col3    col6
test    23      2323    36354
test2   354     35b     555
test4   486     g4      34
test5   0.6     35      34563

Upvotes: 0

Views: 2502

Answers (3)

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can iterate through the fields when the awk is reading the first line and determine which field col6 is present,

NR==1 {
        for (i=1; i<=NF; i++)
                if ($i == "col6")
                        column=i
}
{
        print $1, $2, $3, column ? $column : ""
}

What it does?

  • NR==1 If the current number of records(lines) read is 1, then iterate through NF number of fields

    • if ($i == "col6") if the current column is equal to the string we search, we save it in variable column.
  • print $1, $2, $3, column ? $column : "" Prints the first three fields. The column field is printed only if it is set, if not prints empty "".


Example

$ awk 'NR==1{ for (i=1; i<=NF; i++)if ($i == "col6") column=i}{print $1, $2, $3, column ? $column : ""}' file
col1 col2 col3 col6
test 23 2323 36354
test2 354 35b 555
test4 486 g4 34
test5 0.6 35 34563

Upvotes: 4

James Brown
James Brown

Reputation: 37424

In awk:

$ awk -v p='col6' -F'\t' '
NR==1 {                                 # on the first record
    split($0,a,FS);                     # split header to array a
    for(i in a)                         # search for index of field with p
        if(a[i]==p)
            c=i                         # set it to c
} 
$0=$1 OFS $2 OFS $3 ( c ? OFS $c : "" ) # print $1-$3 and $c if it exists
' foo 
col1    col2    col3    col6
test    23      2323    36354
test2   354     35b     555
test4   486     g4      34
test5   0.6     35      34563

If you want output tab delimited as well, add -v OFS='\t' on the command line.

Upvotes: 2

dormi330
dormi330

Reputation: 1353

cat /your/file | awk 'NR<=3 {print $0}' | grep 'your-pattern'

print the first three column

awk 'NR<=3 {print $0}' # if file has headers , NR<=4

and search for a pattern and print that column if present

grep 'foo'

Upvotes: 2

Related Questions