使用zcat和sed或awk编辑压缩的.gz文本文件 [英] Use zcat and sed or awk to edit compressed .gz text file
问题描述
我正在尝试通过删除第2、6、10、14行的前六个字符来编辑压缩的fastq.gz文本文件...现在,我有两种不同的方式来执行此操作,无论是使用awk还是sed,但是这些文件似乎仅在文件解压缩后才有效.我想在不解压缩文件的情况下编辑文件,并尝试以下代码而不使其工作.谢谢.
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 using awk or sed, but these only seem to work if the files are unzipped. I would like to edit the files without unzipping them and tried the following code without getting it to work. Thanks.
使用sed:
zcat /dir/* | sed -i~ '2~4s/^.\{6\}//'
使用awk:
zcat /dir/* | awk 'NR%4==2 {gsub(/^....../,"")} 1'
推荐答案
您不能绕过压缩,但是可以自动方式将解压缩/编辑/重新压缩链接在一起:
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
如果您对此操作非常有信心,则可以通过在循环主体的末尾添加rm "$f~"
来删除备份文件.
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.
这篇关于使用zcat和sed或awk编辑压缩的.gz文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!