Issue
Let's say I have the following string:
something1: +12.0 (some unnecessary trailing data (this must go))
something2: +15.5 (some more unnecessary trailing data)
something4: +9.0 (some other unnecessary data)
something1: +13.5 (blah blah blah)
How do I turn that into simply
+12.0,+15.5,+9.0,+13.5
in bash?
Solution
You can use awk
and sed
:
awk -vORS=, '{ print $2 }' file.txt | sed 's/,$/\n/'
Or if you want to use a pipe:
echo "data" | awk -vORS=, '{ print $2 }' | sed 's/,$/\n/'
To break it down:
awk
is great at handling data broken down into fields-vORS=,
sets the "output record separator" to,
, which is what you wanted{ print $2 }
tellsawk
to print the second field for every record (line)file.txt
is your filenamesed
just gets rid of the trailing,
and turns it into a newline (if you want no newline, you can dos/,$//
)
Answered By - Dan Fego Answer Checked By - Terry (WPSolving Volunteer)