'Convert text file to csv using shell script

I have a file like this

InputFile.txt
JOB JOB_A
  Source C://files/InputFile
  Resource 0 AC
  User Guest
  ExitCode 0 Success
EndJob
JOB JOB_B
  Source C://files/
  Resource 1 AD
  User Current
  ExitCode 1 Fail
EndJob
JOB JOB_C
  Source C://files/Input/
  Resource 3 AE
  User Guest2
  ExitCode 0 Success
EndJob

I have to convert the above file to a csv file as below

enter image description here

How to convert it using shell scripting?



Solution 1:[1]

with sed ... dont know if the order in the InputFile.txt is always the same as Source, Resource, User, ExitCode, but if it is

declare delimiter=";"
sed -Ez "s/[^\n]*(Source|Resource|User) ([^\n]*)\n/\2${delimiter}/g;s/[ \t]*ExitCode //g;s/[^\n]*JOB[^\n]*\n//gi;s/^/Source${delimiter}Resource${delimiter}User${delimiter}ExitCode\n/" < InputFile.txt > output.csv

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Schmaehgrunza