AWK output to delimited string
The following
awk/\-0.81/ { print $0 }' fullYearData.txt
finds instances successfully
-0.81
In a long list of strings like:
2017-01-10 16:58 0.90 feet Low Tide
2017-01-10 22:54 2.10 feet High Tide
2017-01-11 07:19 -0.81 feet Low Tide
2017-01-11 14:15 1.06 feet High Tide
2017-01-11 17:48 0.89 feet Low Tide
2017-01-11 23:42 2.13 feet High Tide
2017-01-12 08:03 -0.81 feet Low Tide
2017-01-12 14:51 1.06 feet High Tide
Output:
2017-01-11 07:19 -0.81 feet Low Tide
2017-01-12 08:03 -0.81 feet Low Tide
How would I manage to output the result (2 lines in this case) on one line like:
2017-01-11 07:19 -0.81 feet Low Tide, 2017-01-12 08:03 -0.81 feet Low Tide
source to share
$ awk '/-0.81/{if(!a)a=$0; else a=a", "$0} END{print a}' fullYearData.txt
2017-01-11 07:19 -0.81 feet Low Tide, 2017-01-12 08:03 -0.81 feet Low Tide
Or even shorter:
awk '/-0.81/{a=!a ? $0 : a", "$0} END{print a}' fullYearData.txt
Append the appropriate lines to variable ( a
) and in END
, print the variable a
. a
Would initially be zero, and validation if-else
was used to decide when to add ,
(i.e., not the first time, but add ,
for subsequent rows matched by the pattern.
source to share