Domanda

I have two tab-delimited files:

file tmp1.tsv:

1    aaa
2    bbb
3    ccc
4    ddd
5    eee

file tmp2.tsv:

3
2
4

I want to get this:

3    ccc
2    bbb
4    ddd

Using following routine:

$ cat tmp2.tsv | awk -F '\t' <magic here> tmp1.tsv

I know how to make it without stdin:

$ awk -F '\t' 'FNR==NR{ a[$1] = $2; next }{ print $1 FS a[$1] }' tmp1.tsv tmp2.tsv

But have no idea how to make it with stdin. Also, explanation of the solution will be appreciated.

È stato utile?

Soluzione

Assuming your solution works as desired, it is trivial. Instead of:

 awk -F '\t' 'FNR==NR{ a[$1] = $2; next }{ print $1 FS a[$1] }' tmp1.tsv tmp2.tsv

simply do:

 < tmp2.tsv awk -F '\t' 'FNR==NR{ a[$1] = $2; next }{ print $1 FS a[$1] }' tmp1.tsv -

(Note that I've replaced cat tmp2.tsv | with a redirect to avoid UUOC.)

That is, specify a filename of - and awk will read from stdin.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top