[Solved] Read from and append to file at the same time while preserving end-of-line

user56041 Asks: Read from and append to file at the same time while preserving end-of-line
Conceptually I have an easy task… I have [loosely] structured data in a file:

Code:
Testing: debug, default CXXFLAGS
<100's of additional output lines>
Testing: release, default CXXFLAGS
<100's of additional output lines>
...

I try to summarize it in a log file:

Code:
echo "Configurations tested:" | tee -a "$TEST_RESULTS"
echo $($GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: /    * /g') | tee -a "$TEST_RESULTS"

Instead of:

Code:
Configurations tested:
    * debug, default CXXFLAGS
    * release, default CXXFLAGS

I get:

Code:
Configurations tested:
1 3way.cpp 3way.h CMakeLists.txt CMakeLists.txt.diff Doxyfile Filelist.txt GNUmakefile
GNUmakefile-cross Install.txt License.txt Readme.txt TestData TestVectors adhoc.cpp.proto
adler32.cpp adler32.h aes.h algebra.cpp algebra.h ...

I think I am wreaking havoc on the file buffer $TEST_RESULTS because its being read from in the grep, and written to with the tee.

When I attempt to put the result of $GREP 'Testing: ' "$TEST_RESULTS" | $SED 's/Testing: / * /g' in a shell variable, I loose the line endings which results in one big concatenation:

Code:
* debug, default CXXFLAGS    * release, default CXXFLAGS ... <30 additional configs>

How do I read from and append to a file at the same time while preserving the end-of-lines?


I’ve made some progress with:

Code:
ESCAPED=$($GREP 'Testing: ' "$TEST_RESULTS" | $AWK -F ": " '{print "  -" $2 "$"}')
echo $ESCAPED | tr $ 'n' | tee -a "$TEST_RESULTS"

However, it can’t use * as a bullet point, and it seems to drop leading space:

Code:
Configurations tested:
-debug, default CXXFLAGS
 -release, default CXXFLAGS

I’m not using sed because swapping-in a new line is an absolute pain across platforms. Platforms include BSD, Cygwin, Linux, OS X, Solaris.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.