I see this being answered here: Moving matching lines in a text file using sed and here: How to move specified line in file to other place with regexp match (bash script)?
But I am having problems implementing it for my case.
I have a .csv file that contains:
Unit Tests
Model: A
Tue Sep 8 22:52:24 MDT 2020
User: br
Files Used:
DATA FILE - /path/to/file/data.txt
TEST TARBALL - /path/to/tarball/my_test.tar.bz2
Results:
Module,Test Suite,Total Tests,Tests Passed,Tests Failed,Pass Rate
Individual Test Run,ClientTests,15,15,0,100.00%
,GraphTests,37,26,11,70.00%
Mean Pass Rate for All Tests,,52,41,11,78.85%
I need to move the:
Mean Pass Rate for All Tests,,52,41,11,78.85%
up to right below "Results". So it would look like this:
Unit Tests
Model: A
Tue Sep 8 22:52:24 MDT 2020
User: br
Files Used:
DATA FILE - /path/to/file/data.txt
TEST TARBALL - /path/to/tarball/my_test.tar.bz2
Results:
Mean Pass Rate for All Tests,,52,41,11,78.85%
Module,Test Suite,Total Tests,Tests Passed,Tests Failed,Pass Rate
Individual Test Run,ClientTests,15,15,0,100.00%
,GraphTests,37,26,11,70.00%
The line numbers are not guaranteed. The "Files Used" section may include more files. Also, there are possibly other "Mean Pass Rate" lines depending on the tests being ran. For example:
Unit Tests
Model: A
Tue Sep 8 22:52:24 MDT 2020
User: br
Files Used:
DATA FILE - /path/to/file/data.txt
TEST TARBALL - /path/to/tarball/my_test.tar.bz2
CLIENT FILE - /path/to/other/file/client.txt
Results:
Module,Test Suite,Total Tests,Tests Passed,Tests Failed,Pass Rate
Individual Test Run,ClientTests,15,15,0,100.00%
,ClientVarTests,12,12,0,100.00%
,GraphTests,37,26,11,70.00%
Mean Pass Rate for Client Tests,,27,27,0,100.00%
Mean Pass Rate for All Tests,,64,53,11,82.81%
In my script I tried awk:
MEAN_PASS_ALL="Mean Pass Rate for All Tests"
awk '/${MEAN_PASS_ALL}/{t=$0;next}1;/RESULTS/{print t}' ${CSV_FILE}
However all that did was print out the .csv file contents and did not move the line:
$ ./parse_results.sh
Unit Tests
Model: A
Tue Sep 8 22:52:24 MDT 2020
User: br
Files Used:
DATA FILE - /path/to/file/data.txt
TEST TARBALL - /path/to/tarball/my_test.tar.bz2
Results:
Module,Test Suite,Total Tests,Tests Passed,Tests Failed,Pass Rate
Individual Test Run,ClientTests,15,15,0,100.00%
,GraphTests,37,26,11,70.00%
Mean Pass Rate for All Tests,,52,41,11,78.85%
I also tried sed, but that gave errors:
sed "/.*Mean/d;/.*RESULTS/i"$(sed -n '/.*Mean/p' ${CSV_FILE}) ${CSV_FILE}
$ ./parse_results.sh
sed: can't read Pass: No such file or directory
sed: can't read Rate: No such file or directory
sed: can't read for: No such file or directory
sed: can't read All: No such file or directory
sed: can't read Tests,,52,41,11,78.85%: No such file or directory
Unit Tests
Model: A
Tue Sep 8 22:52:24 MDT 2020
User: br
Files Used:
DATA FILE - /path/to/file/data.txt
TEST TARBALL - /path/to/tarball/my_test.tar.bz2
Results:
Module,Test Suite,Total Tests,Tests Passed,Tests Failed,Pass Rate
Individual Test Run,ClientTests,15,15,0,100.00%
,GraphTests,37,26,11,70.00%
It looks like it removed the line, but it did not print it to where I want it, nor did it "save" the change.
If there are more than one line that contain "Mean Pass Rate", I would like them all moved to right below the "Results:"
What am I doing wrong here? How do I move the lines in the file and have the file saved with the changes?
Thanks!