My input files are file_1.txt, file_2.txt, file_3.txt and so on. These files contains data such as
$ head file_*.txt
==> file_1.txt <==
----- Reset Loop 1 -------
Test #1
data
Test #2
data
Test #3
Test #4
data
==> file_2.txt <==
----- Reset Loop 2 -------
Test #1
Test #2
data
Test #3
Test #4
data
==> file_3.txt <==
----- Reset Loop 3 -------
Test #1
data
Test #2
data
Test #3
Test #4
The code I have right now to get filename and the sequence number behind Test only if the data available under each Test from the input file is:
#!/bin/bash
awk '
FNR==1 {
testId = ""
split(FILENAME,f,/[_.]/)
fileId = f[4]
}
testId != "" {
if (NF) {
print testId > "1_val.txt"
print fileId > "2_val.txt"
}
testId = ""
}
sub(/^Test #/,"") {
testId = $0
}
' file_*.txt
The output i get from this code:
1_val.txt
1
2
4
2
4
1
2
2_val.txt
ਲਲਲਲਲਲਲਲਲਲਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਲ਼ਵਵਵਵਵਵਵਵਵਵ
There might be some problem in my OS or something else as there are weird characters in my output file. I have thought of an alternative of getting the number in the first line of the input file for the data listed in 1_val.txt.
The code I have for this is awk 'NR==1' file_*.txt
but I am not so sure where to insert this particular command in the script.
The expected output:
2_val.txt
1
1
1
2
2
3
3
Edit: This is the exact command I am running to create the output files.
thulasyc > cat data_collect.sh
#!/usr/bin/env bash
awk '
FNR==1 {
testId = ""
fileId = $4
}
testId != "" {
if (NF) {
print testId > "1_val.txt"
print fileId > "2_val.txt"
}
testId = ""
}
sub(/^TX PTP Command #/,"") {
testId = $0
}
' "${@:--}"
thulasyc > ./data_collect.sh ptp_log_reset_*.txt
thulasyc > head *_val*
==> 1_val.txt <==
1
2
3
5
6
11
12
13
15
16
==> 2_val.txt <==
1
1
1
1
1
1
1
1
1
1
The display of the content of the output files: