RobynP
August 30, 2017, 9:53am
1
Hello all,
Hopefully this should be an easy one for you. I have a file that contains a list of parameters and their values. Some of these values are 2D arrays split over several lines, I need to move these so the parameter name and it's full value is all on one line. The parameters are separated by semi-colons.
That is I have a file that looks like the following:
param_a = 1 2;
param_b = 3 4
5 6;
param_c = 5;
param_d = 6
7
8
9;
etc
What I need for output is
param_1 = 1 2;
param_b = 3 4 5 6;
param_c = 5;
param_d = 6 7 8 9;
I have the following so far but it doesn't work as it ignores the lines that contain the semi-colon.
awk '/;/{if (NR!=1)print "";next}{printf "%s ",$0}END{print "";}' testfile >> testfileout
something like this:
awk '{printf("%s%s", $0, (/;/)?ORS:"")}' myFile
RobynP
August 30, 2017, 10:04am
3
That does the job perfectly. Thanks. Any chance you could explain it? My awk is terrible
Also would I need to pipe the output to a new file or is there any way to make the changes directly in the original file?
awk '{printf("%s%s", $0, (/;/)?ORS:" ")}' myFile
For each record/line, printf
with 2 parameters (%s%s):
First parameter is the line/record itself ($0)
For the second parameter, if the line contains a ;
, output ORS (by default EndOfLine); if not - output a space " "
To save result in the same files, there're many ways to skin that cat:
awk '{printf("%s%s", $0, (/;/)?ORS:"")}' myFile >| /tmp/$$ && mv /tmp/$$ myFile
OR
{rm myFile; awk '{printf("%s%s", $0, (/;/)?ORS:"")}' >| myFile; } < myFile
RobynP
August 30, 2017, 10:21am
5
Thanks. really appreciated.
RudiC
August 30, 2017, 12:02pm
6
Try also
awk '{while (!(/;/)) {getline X; $0=$0 X}} 1' file