Usando o AWK:
awk -v nlines=$(wc -l test.txt | cut -d ' ' -f 1) '$0=="Reading input from PoolA_Rnase", $0=="Reading input from PoolB_Rnase" {if($0 != "Reading input from PoolB_Rnase") {print >"header1.txt"}} $0=="Reading input from PoolB_Rnase", NR==nlines-4 {print >"header2.txt"}' test.txt
Script AWK expandido e comentado:
-
nlines
contém o número de linhas no arquivo, calculado via $(wc -l test.txt | cut -d ' ' -f 1)
.
$0=="Reading input from PoolA_Rnase", $0=="Reading input from PoolB_Rnase" { # if the current record is between a record matching "Reading input from PoolA_Rnase" and a record matching "Reading input from PoolB_Rnase" inclusive
if($0 != "Reading input from PoolB_Rnase") { # if the current record doesn't match "Reading input from PoolB_Rnase"
print >"header1.txt" # prints the record to header1.txt
}
}
$0=="Reading input from PoolB_Rnase", NR==nlines-4 { # if the current record is between a record matching "Reading input from PoolB_Rnase" and the record number 'nlines-4' inclusive
print >"header2.txt" # prints the record to header2.txt
}
% cat test.txt
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
line 21
line 22
line 23
line 24
line 25
Reading input from PoolA_Rnase
foo
foo
foo
Reading input from PoolB_Rnase
bar
bar
bar
line 1
line 2
line 3
line 4
% awk -v nlines=$(wc -l test.txt | cut -d ' ' -f 1) '$0=="Reading input from PoolA_Rnase", $0=="Reading input from PoolB_Rnase" {if($0 != "Reading input from PoolB_Rnase") {print >"header1.txt"}} $0=="Reading input from PoolB_Rnase", NR==nlines-4 {print >"header2.txt"}' test.txt
% cat header1.txt
Reading input from PoolA_Rnase
foo
foo
foo
% cat header2.txt
Reading input from PoolB_Rnase
bar
bar
bar