user_D_A__
user_D_A__

Reputation: 460

awk command to merge the content of the same file

I have an input file with the following content

1 1
2 1
3 289
4 1
5 2 
0   Clear
1   Warning
2   Indeterminate
3   Minor
4   Major
5   Critical

I want to merge the first type of lines with the messages by the first column and obtain

  1 1 Warning
  2 1 Indeterminate
  3 289 Minor 
  4 1 Major
  5 2 Critical

Upvotes: 2

Views: 77

Answers (3)

John B
John B

Reputation: 3646

You can do this with Awk:

awk 'BEGIN{n=0}NR>6{n=1}n==0{a[$1]=$2}n==1{print $1,a[$1],$2}' file

or another way:

awk 'NR<=5{a[$1]=$2}$2~/[a-zA-z]+/ && $1>0 && $1<=5{print $1,a[$1],$2}' file

Upvotes: 2

konsolebox
konsolebox

Reputation: 75458

Just use awk:

awk '$1 in a { print $1, a[$1], $2; next } { a[$1] = $2 }' file

Output:

1 1 Warning
2 1 Indeterminate
3 289 Minor
4 1 Major
5 2 Critical

Upvotes: 5

perreal
perreal

Reputation: 97928

Using join/sed, sed creates different views of the file for each part and join joins on the common field:

join <(sed '/^[0-9]* [0-9]* *$/!d' input) <(sed '/^[0-9]* [0-9]* *$/d' input)

Gives:

1 1 Warning
2 1 Indeterminate
3 289 Minor
4 1 Major
5 2  Critical

Upvotes: 2

Related Questions