I want to read a multi-line file in a bash
script, using the file path from a variable, then merge the lines using a multi-char delimiter and save the result to another variable.
I want to skip blank lines and trailing new lines and do not want a trailing delimiter.
Additionally I want to support rn
and – if at no further “cost” – why not also r
as line break (and of course n
).
The script should run on RHEL with GNU’s bash 4.2.46, sed 4.2.2, awk 4.0.2, grep 2.20, coreutils 8.22 (tr, cat, paste, sort, cut, head, tail, tee, …), xargs 4.5.11 and libc 2.17 and with perl 5.16.3, python 2.7.5 and openjdk 11.0.8.
It should run about twice per day on files with ca. 10 lines on a decent machine/VM.
If readability, maintainability and brevity don’t suffer too much I’m very open to more performant solutions though.
The files to be read from can be created and modified either on the same machine or other Win7
or Win10
systems.
My approach so far is
joined_string_var=$(sed 's/r/n/g' $filepathvar | grep . | sed ':a; N; $!ba; s/n/; /g')
-
So first I replace
r
withn
to cover all newline formats and make the output readable for grep. -
Then I remove blank lines with
grep .
-
And finally I use sed for the actual line merging.
I used sed
instead of tr
in the first step to avoid using cat, but I’m not quite sure if I prefer it like that:
joined_string_var=$(cat $filepathvar | tr 'r' 'n' | grep . | sed ':a; N; $!ba; s/n/; /g')
UPDATE: I somehow completely missed simple redirection:
joined_string_var=$(tr 'r' 'n' <$filepathvar | grep . | sed ':a; N; $!ba; s/n/; /g')
Any thoughts how this might be done more elegantly (less commands, better performance, not much worse brevity and readability)?