How to get few lines from a .gz compressed file without uncompressing

后端 未结 5 1190
野的像风
野的像风 2020-12-22 22:28

How to get the first few lines from a gziped file ? I tried zcat, but its throwing an error

zcat CONN.20111109.0057.gz|head
CONN.20111109.0057.gz.Z: A file o         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 22:53

    This awk snippet will let you show not only the first few lines - but a range you can specify. It will also add line numbers which i needed for debugging an error message pointing to a certain line way down in a gzipped file.

    gunzip -c file.gz | awk -v from=10 -v to=20 'NR>=from { print NR,$0; if (NR>=to) exit 1}'
    

    Here is the awk snippet used in the one liner above. In awk NR is a built-in variable (Number of records found so far) which usually is equivalent to a line number. the from and to variable are picked up from the command line via the -v options.

    NR>=from {
       print NR,$0; 
       if (NR>=to) 
         exit 1
    }
    

提交回复
热议问题