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

I am trying to edit fastq.gz compressed text files by deleting the first six characters of lines 2,6,10,14 ... I have two different ways to do this right now using awk or sed, but they only work if files are unpacked. I would like to edit the files without unpacking them, and tried the following code without making it work. Thank.

Using sed:

zcat /dir/* | sed -i~ '2~4s/^.\{6\}//'

Using awk:

zcat /dir/* | awk 'NR%4==2 {gsub(/^....../,"")} 1'
+4
source share
1 answer

You cannot bypass compression, but you can combine unpacking / editing / re-compression in automatic mode:

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

, , rm "$f~" .

+13

All Articles