Search code examples
linuxshellawkpathzcat

Zcat + awk with absolute path


In my "test.tar.gz" there is a text file : "test.txt" with :

col1 {tab} col2 {tab} col3

-

The problem is, when I run it :

zcat ./folder1/test.tar.gz | awk -F '\t' '{print $1, $3}'

It returns :

test.txt

So if I add :

zcat ./folder1/test.tar.gz | awk -F '\t' '{print $1, $3}' test.txt

It returns :

awk: cannot open test.txt (No such file or directory)

Thanks by advance !


Solution

  • Try using something like this:

    tar -xzOf test.tar.gz test.txt | awk '{print $1, $3}'
    

    This extracts the file test.txt from the archive. The -O switch sends the contents of the file to standard output, which can then be piped to awk.