Adding a comment in a file next to data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a comment in a file next to data
# 1  
Old 11-07-2016
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
Code:
<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

Code:
<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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-07-2016 at 07:56 AM.. Reason: Added CODE tags.
# 2  
Old 11-07-2016
For exactly that sample, try
Code:
awk -F";" '/^<.*>$/ {PRFX = $1 ","; print; next} {sub (/^/, PRFX)}1' OFS=";" file

# 3  
Old 11-07-2016
Hi

I tryied running but it is coming out with an error

Code:
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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-07-2016 at 08:42 AM.. Reason: Added CODE tags.
# 4  
Old 11-07-2016
can you try the below one for given input/output sample ?

Code:
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 ?

Last edited by greet_sed; 11-07-2016 at 08:20 AM.. Reason: Add space
# 5  
Old 11-07-2016
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

Code:
<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"

# 6  
Old 11-07-2016
Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk.
# 7  
Old 11-07-2016
Hi,

Seems it was not in post#1 .

can you try the bleow one ?
Code:
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 Smilie His solution works as said before.

Last edited by greet_sed; 11-07-2016 at 08:48 AM.. Reason: Add text.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please. Feb 8 2014 15 Feb 10 2014 32 Feb 10 2014 32 Feb 12... (2 Replies)
Discussion started by: makauser
2 Replies

2. Red Hat

Adding a data file

I need to add a data file, but my user account doesn't have privileges. I have connected to the oracle as sqlplus / as sysdba and it is telling me I'm connect to an idle instance. How can I connect to my database and create a data file if I don't know the passwords of any of the account that have... (1 Reply)
Discussion started by: FaSho76
1 Replies

3. Shell Programming and Scripting

Help in adding a data after a particular line of data in a file.

Hi.. I'm into a bump after trying to solve this prob.. i've a file with contents like below. <blankline> 'pgmId' : 'UNIX', 'pgmData' : 'textfile', 'author' : 'admin', ....... Now i'm trying to insert a new data after pgmId. so the final output will be... (7 Replies)
Discussion started by: arjun_arippa
7 Replies

4. Shell Programming and Scripting

adding data in input file if 2nd script output SUCCESS

Hi All, how can i edit my original data and add more data if my 2nd script outputs SUCESS? ex. input file: 1.txt nik,is,the 1script.sh if 2ndscript.sh output SUCCESS then i'm going to edit my input file and add data best,pogi.. sample outputdata. nik,is,the,best,pogi 2ndscript.sh... (3 Replies)
Discussion started by: nikki1200
3 Replies

5. Shell Programming and Scripting

Adding the data before file extension

HI , I have a file with multiple lines like below, I need to check on column starting with #prototype if it has the .Number.txt extension do nothing else add 1.txt extension. Source data Jdbc_app_data :- sample data #prototype= sample data from test.1.txt application_id=122135... (4 Replies)
Discussion started by: gaur.deepti
4 Replies

6. Shell Programming and Scripting

Adding data in a file on same line

Hi, I have one file a.txt ,the contents of the file is A B C D E F and I have another file b.txt, the contents of the file is 1 2 3 4 5 6 now when I am using this command cat a.txt b.txt > c.txtI am getting the output as A B C D E F 1 2 3 4 5 6 but i need the output... (2 Replies)
Discussion started by: prarat
2 Replies

7. Shell Programming and Scripting

sed adding/removing comment in crontab

I have a requirement where I want to add a comment '#' in my crontab, run a process, than remove the '#' I added. Example cron #5,10 * * * * ls -lt /tmp 10,5 * * * * ls -lt /var I would like to be able use sed or awk to add a '#' at the begining of each line. After the command... (4 Replies)
Discussion started by: BeefStu
4 Replies

8. Shell Programming and Scripting

Pulling data from a standard comment block - perl

OK so I've inherited a set of scripts that do some work on a database. They do all have a standard comment block at the beginning that has good information on the script. I would like to generate a quick web page report that lists the script name and the description lines (for now it may be... (1 Reply)
Discussion started by: khuilman
1 Replies

9. Shell Programming and Scripting

adding the data at a specified location in a file....

Hi all, I m new to shell programming..Can anyone please guide me how to insert data at a specified location in the file.. I have a configuration file..I want to add data to it through script..I am able to do it...I get that data written at end of my configuration file..I want data to be placed at... (3 Replies)
Discussion started by: divya_flora
3 Replies

10. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies
Login or Register to Ask a Question