我有多个文本文件,例如
样本1.tsv
## index of replication (iRep) - thresholds: min cov. = 5, min wins. = 0.98, min r^2 = 0.9, max fragments/Mbp = 175, GC correction min r^2 = 0.0
# genome ./bam/18097D-02-01.sam.sorted.sam
18097D-02-01_bin.11.fa 1.295179372
18097D-02-01_bin.13.fa 1.284880274
18097D-02-01_bin.15.fa 1.339609918
#
## un-filtered index of replication (iRep)
# genome ./bam/18097D-02-01.sam.sorted.sam
18097D-02-01_bin.11.fa 1.295179372
18097D-02-01_bin.13.fa 1.284880274
18097D-02-01_bin.15.fa 1.339609918
#
## raw index of replication (no GC bias correction)
# genome ./bam/18097D-02-01.sam.sorted.sam
18097D-02-01_bin.11.fa 1.298934455
18097D-02-01_bin.13.fa 1.2885746
#
样本2.tsv
## index of replication (iRep) - thresholds: min cov. = 5, min wins. = 0.98, min r^2 = 0.9, max fragments/Mbp = 175, GC correction min r^2 = 0.0
# genome ./bam/18097D-02-02.sam.sorted.sam
18097D-02-02_bin.11.fa 1.59665286
18097D-02-02_bin.13.fa 1.332990306
18097D-02-02_bin.14.fa 1.499196606
18097D-02-02_bin.6.fa 1.323465715
18097D-02-02_bin.9.fa 1.583302299
#
## un-filtered index of replication (iRep)
# genome ./bam/18097D-02-02.sam.sorted.sam
18097D-02-02_bin.11.fa 1.59665286
18097D-02-02_bin.13.fa 1.332990306
18097D-02-02_bin.14.fa 1.499196606
18097D-02-02_bin.6.fa 1.323465715
18097D-02-02_bin.9.fa 1.583302299
#
## raw index of replication (no GC bias correction)
# genome ./bam/18097D-02-02.sam.sorted.sam
18097D-02-02_bin.11.fa 1.603339021
18097D-02-02_bin.13.fa 1.366124796
18097D-02-02_bin.14.fa 1.502052999
18097D-02-02_bin.6.fa 1.324573575
18097D-02-02_bin.9.fa 1.618136032
#
我想从第一个 ## 到第二个 ## 的所有文件中提取所有内容,这意味着我想要输出 -
## index of replication (iRep) - thresholds: min cov. = 5, min wins. = 0.98, min r^2 = 0.9, max fragments/Mbp = 175, GC correction min r^2 = 0.0
# genome ./bam/18097D-02-01.sam.sorted.sam
**18097D-02-01_bin.11.fa 1.295179372
18097D-02-01_bin.13.fa 1.284880274
18097D-02-01_bin.15.fa 1.339609918**
#
## un-filtered index of replication (iRep)
## index of replication (iRep) - thresholds: min cov. = 5, min wins. = 0.98, min r^2 = 0.9, max fragments/Mbp = 175, GC correction min r^2 = 0.0
# genome ./bam/18097D-02-02.sam.sorted.sam
**18097D-02-02_bin.11.fa 1.59665286
18097D-02-02_bin.13.fa 1.332990306
18097D-02-02_bin.14.fa 1.499196606
18097D-02-02_bin.6.fa 1.323465715
18097D-02-02_bin.9.fa 1.583302299**
#
## un-filtered index of replication (iRep)
我试图这样做,但没有输出 sed -n '/##=/{s/.#=//;s/\S=.*//;p}' *.tsv > ../test.tsv
我其实想要——
18097D-02-01_bin.11.fa 1.295179372
18097D-02-01_bin.13.fa 1.284880274
18097D-02-01_bin.15.fa 1.339609918
18097D-02-02_bin.11.fa 1.59665286
18097D-02-02_bin.13.fa 1.332990306
18097D-02-02_bin.14.fa 1.499196606
18097D-02-02_bin.6.fa 1.323465715
18097D-02-02_bin.9.fa 1.583302299
谢谢
答案1
使用awk
:
awk 'FNR==1{p=0}
FNR==1 && NR>1 {print ""}
$0 ~ /^##/ {p++}
p==1 && $0 !~ /^#/
' sample*
输出:
18097D-02-01_bin.11.fa 1.295179372
18097D-02-01_bin.13.fa 1.284880274
18097D-02-01_bin.15.fa 1.339609918
18097D-02-02_bin.11.fa 1.59665286
18097D-02-02_bin.13.fa 1.332990306
18097D-02-02_bin.14.fa 1.499196606
18097D-02-02_bin.6.fa 1.323465715
18097D-02-02_bin.9.fa 1.583302299
解释:
FNR==1{p=0}
将每个新文件的指针设置为 0(FNR
是当前文件的行号)FNR==1 && NR>1 {print ""}
为除第一个文件之外的每个文件打印空行$0 ~ /^##/ {p++}
当行以##开头时,增加指针p==1 && $0 !~ /^#/
##
如果指针为 1(这是从第一个到第二个的情况##
,并且该行不以 a 开头#
,则打印它。
答案2
#!/bin/bash
sample_dir="/C/Users/testuser/Desktop/sample"
out_file="/C/Users/testuser/Desktop/sample/output.tsv"
for file in "$sample_dir"/*
do
count=0
while read line; do
if [[ "$line" == "##"* ]] || [[ "$line" == "#"* ]]; then
count=$((count+1))
if [[ count -ge 4 ]]; then
echo -e "" >> $out_file
continue 2
fi
continue
else
echo -e "$line" >> $out_file
fi
done < $file
done