Reputation: 317
Hello guys need some help with this. As a curl output to the file I have the following:
FINISHED
JOB#1
20140428 0016
FINISHED
JOB#2
20140428 0015
Is there a way to merge these lines the following way:
JOB#1;0015;20140428;FINISHED
JOB#2;0016;20140428;FINISHED
JOB#3;0017;20140428;FINISHED
And so on...
I tried:
paste -d, -s filenew.com
Upvotes: 0
Views: 612
Reputation:
posix awk supports getline
so:
$ awk --posix -v OFS=';' '
{Status = $0; getline Job; getline; Date = $1; Time = $2;
print Job, Time, Date, Status;}' file.txt
JOB#1;0016;20140428;FINISHED
JOB#2;0015;20140428;FINISHED
Upvotes: 1
Reputation: 58473
This might work for you (GNU sed):
sed -r 'N;N;s/(.*)\n(.*)\n(.*) (.*)/\2;\3;\4;\1/' file
Read 3 lines in at a time and re-arrange the contents.
Upvotes: 1
Reputation: 189648
Here's another variation.
tr \n' ';' <file | sed 's/\(;FINISHED\);/\1\n/g'
However, some legacy sed
implementations would choke on long input lines (ISTR old BSD would segfault already on lines longer than 256 characters).
Upvotes: 0
Reputation: 41460
Here is a simple, portable awk
version:
awk '/^2014/ {print x,$2,$1,y} {y=x;x=$0}' OFS=";" file
JOB#1;0016;20140428;FINISHED
JOB#2;0015;20140428;FINISHED
Upvotes: 0
Reputation:
Serg12, I'm assuming you have a typo and that you meant that the output should be:
JOB#1;0016;20140428;FINISHED
JOB#2;0015;20140428;FINISHED
i.e., 0016 on the first line and 0015 in the second one. With sed you can also do:
sed -n "/FINISHED/ n;h;N;s/\(.*\)\n\(.*\) \(.*\)/\1;\3;\2;FINISHED/p" file
Hope it helps.
Upvotes: 0
Reputation: 439247
With gawk
(GNU awk) or mawk
:
awk -v RS='FINISHED' -v OFS=';' '$0 { print $1, $3, $2, RS }' file
Sadly, this won't work with FreeBSD/OSX awk
or strictly POSIX-compliant versions, because they don't support multi-character input-record separators (RS
).
Upvotes: 1
Reputation: 172
BEGIN { finished=""; job=""; ff1=""; ff2=""; }
{
if(finished == "") { finished = $0""; next; }
if(job == "") { job = $0""; next; }
if(ff1 == "") { ff1 = $2""; ff2 = $1""; printf("%s;%s;%s;%s\n", job,ff1,ff2,finished);
finished="";job="";ff1="";ff2="";
}
}
END { }
awk -f formatter.awk inputfile
Upvotes: 1
Reputation: 781721
awk '/^FINISHED/ && job { printf("%s;%s;%s;%s\n", job, num, date, $0); job = "" }
/^JOB/ { job = $0 }
/^[0-9]+ [0-9]+$/ { num = $2; date = $1; }
END { if (job) { printf("%s;%s;%s;%s\n", job, num, date, $0); } }'
Upvotes: 0