Adding a comment in a file next to data

Dear Gurus,

I have a file which comes every day with set of data, as a part of processing i want to add a comment at the start of every line.

e.g of file

<PCL>
2E;"HCA";"COP Car A";"ODBS_CFG"
7C;"DD";"Doors Car D";"ODBS_CFG"
3D;"XA";"Auxiliary Car A";"ODBS_CFG"
3E;"XB";"Auxiliary Master";"ODBS_CFG"
<SUBSYSTEM>
06;"Auxiliary";"Auxiliaries"
0D;"PV&HVAC";"HVAC & PV"
0A;"Comms";"Communications"
<EVENT_LOCATION>
00;"0";"---"
01;"1";"A-car"
02;"2";"B-car"

I want to add "<>" block at the start of the line.

from the above example the output has to be like the below one

<PCL>
<PCL>,2E;"HCA";"COP Car A";"ODBS_CFG"
<PCL>,7C;"DD";"Doors Car D";"ODBS_CFG"
<PCL>,3D;"XA";"Auxiliary Car A";"ODBS_CFG"
<PCL>,3E;"XB";"Auxiliary Master";"ODBS_CFG"
<SUBSYSTEM>
<SUBSYSTEM>06;"Auxiliary";"Auxiliaries"
<SUBSYSTEM>0D;"PV&HVAC";"HVAC & PV"
<SUBSYSTEM>0A;"Comms";"Communications"
<EVENT_LOCATION>
<EVENT_LOCATION>00;"0";"---"
<EVENT_LOCATION>01;"1";"A-car"
<EVENT_LOCATION>02;"2";"B-car"

the text between <> will be add to the line.

Please help

For exactly that sample, try

awk -F";" '/^<.*>$/ {PRFX = $1 ","; print; next} {sub (/^/, PRFX)}1' OFS=";" file

Hi

I tryied running but it is coming out with an error

tdc2solv112:kumarra# awk -F";" '/^<.*>$/ {PRFX = $1 ","; print; next} {sub (/^/, PRFX)}1' OFS=";" ED_D_VLU______002_005_002_000_EN.oti
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

what is wrong i am doing here

can you try the below one for given input/output sample ?

awk '/^</{if($0 ~ /PCL/) a=$0",";else a=$0;print $0; next} {print a $0}' file

It gives desired output as mentioned in post#1.

@ RudiC: Aren't you adding comma , in other <> tags than PCL ?

Hello Greet_sed,

Your command is running fine and excellent, I am not good with unix and was trying to modify it.

You have put ',' after <pcl> i want to put comma after every comment

<PCL>
<PCL>,2E;"HCA";"COP Car A";"ODBS_CFG"
<PCL>,7C;"DD";"Doors Car D";"ODBS_CFG"
<PCL>,3D;"XA";"Auxiliary Car A";"ODBS_CFG"
<PCL>,3E;"XB";"Auxiliary Master";"ODBS_CFG"
<SUBSYSTEM>
<SUBSYSTEM>,06;"Auxiliary";"Auxiliaries"
<SUBSYSTEM>,0D;"PV&HVAC";"HVAC & PV"
<SUBSYSTEM>,0A;"Comms";"Communications"
<EVENT_LOCATION>
<EVENT_LOCATION>,00;"0";"---"
<EVENT_LOCATION>,01;"1";"A-car"
<EVENT_LOCATION>,02;"2";"B-car"

Hi,

Seems it was not in post#1 .

can you try the bleow one ?

awk '/^</{a=$0",";print $0; next} {print a $0}'  file

It gives output posted in # 5.

Seems RudiC read your mind correctly in his first post itself :b: His solution works as said before.

Well - sometimes my mind reading works, and sometimes - the majority of times - it doesn't.

BTW, what I proposed can be simplified (as no FS nor OFS need to be defined) to:

awk  '/^<.*>$/ {PRFX = $0 ","; print; next} {sub (/^/, PRFX)}1'  file

Hi

Thanks for help, it work as expected

---------- Post updated at 09:40 AM ---------- Previous update was at 08:19 AM ----------

Hi

When working with original file it just print ;

it is not printing content of the file. i just gave the few lines from the file. any reason?

More details, please!

Hi

I ran the below command

awk '/^</{a=$0";";print $0; next} {print a $0}' ED_D_SSL______004_006_001_000_EN.oti>ED_D_VLU.txt

the output it shows as

vi ED_D_VLU.txt
"ED_D_VLU.txt" 10990 lines, 10993 characters
\377\376<

So you say it's an empty file? What's the file'e encoding? What you post seems like the introductory bytes of a UTF-16 file to me.

Hi

Working fine your solution, there was a probelm in the file it had hexadecimal character so was not working