Use zcat and sed or awk to edit compressed .gz text file

前端 未结 2 1032
梦如初夏
梦如初夏 2020-12-29 11:05

I am trying to edit compressed fastq.gz text files, by removing the first six characters of lines 2,6,10,14... I have two different ways of doing this right now, either usin

相关标签:
2条回答
  • 2020-12-29 11:40

    You can't bypass compression, but you can chain the decompress/edit/recompress together in an automated fashion:

    for f in /dir/*; do
      cp "$f" "$f~" &&   
      gzip -cd "$f~" | sed '2~4s/^.\{6\}//' | gzip > "$f"
    done
    

    If you're quite confident in the operation, you can remove the backup files by adding rm "$f~" to the end of the loop body.

    0 讨论(0)
  • 2020-12-29 11:47

    I wrote a script called zawk which can do this natively. It's similar to glenn jackman's answer to a duplicate of this question, but it handles awk options and several different compression mechanisms and input methods while retaining FILENAME and FNR.

    You'd use it like:

    zawk 'awk logic goes here' log*.gz
    

    This does not address sed's "in-place" flag (-i).

    0 讨论(0)
提交回复
热议问题