2

My Files:

EventTime=20180726001317
ObjectOfReference=TCU_XXX202
SPtext=Heartbeat Failure
EAName=TCU_XXX202
EventTime=20180726001317
ObjectOfReference=TCU_XXX203
SPtext=Heartbeat Failure
EAName=TCU_XXX203

Expected Output:

EventTime;ObjectOfReference;SPtext;EAName
20180726001317;TCU_XXX202;Heartbeat Failure;TCU_XXX202
20180726001317;TCU_XXX203;Heartbeat Failure;TCU_XXX203

I tried with awk script but it's not working:

awk -F\"= :\" '!/%a/&&/-EventTime|EAName|-ObjectOfReference|-SPtext|%A/{if(\$1 != \"%A\")printf \$NF\",\";else print \"\\n\"}'
3

With GNU awk:

awk -F= '{a=(FNR!=NR)+1;printf "%s"(NR%4==0?RS:";"),$a} a==1&&NR>=4{nextfile}' file file

However if you don't need the header line, this will also do the job (and work with mawk too):

awk -F= '{printf "%s"(NR%4==0?RS:";"),$2}' file

Explanation:

  • -F= sets the field separator to =.
  • a=(FNR!=NR)+1: $a is set to 1 in the first file and 2 in the second one.
  • printf "%s"(NR%4==0?RS:";"),$a: print the string and if it's a 4th line print the row separator RS, else print the field separator ;
  • a==1&&NR>=4: if we are in the first file and 4 lines are processed ...
  • nextfile ... start processing the next file (this is GNU specific)

Output:

EventTime;ObjectOfReference;SPtext;EAName
20180726001317;TCU_XXX202;Heartbeat Failure;TCU_XXX202
20180726001317;TCU_XXX203;Heartbeat Failure;TCU_XXX203

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.