Using gsub with xml formats


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using gsub with xml formats
# 1  
Old 06-16-2016
Using gsub with xml formats

Hello,

I'm looking again for your precious help.

I'm running an IBM AIX box.
I have 'wrong' xml files and waiting for the bug correction I need to manipulate them
The problem is a missing tag.
Now you know that xmls are full of < and > and \ so our scripts just go beserk.
To be on the safe side I want to insert a tag between two known tags:

wrong data
Code:
<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

correct data
Code:
<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<AviExp>123456</AviExp>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

Instead of using while...read... I tried gsub, but all those fancy delimiters just don't let the command to work properly. Furthermore there are CRLF that are to be taken into account.

This is my try:
Code:
awk '{gsub("<FullPallet>N</FullPallet>\n<BlockCode>0</BlockCode>","<FullPallet>N</FullPallet>\n<AviExp>123456</AviExp>\n<BlockCode>0</BlockCode>");printf"%s",$0}' S210404890.xml > test.txt

Any help is really appreciated.

thanks,
Ema
# 2  
Old 06-16-2016
Hello emare,

Could you please try following and let me know how it goes then.
Code:
awk '{gsub(/<\/FullPallet>/,"</FullPallet>\n<AviExp>123456</AviExp>",$0)} 1'  Input_file

Output will be as follows.
Code:
<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<AviExp>123456</AviExp>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

Thanks,
R. Singh
# 3  
Old 06-16-2016
Thanks RavinderSingh, it went OK
But I need to check both tags <FullPallet>.... and <BlockCode>.... because the tag <AviExp>.... can already be there and so I must not add it twice.

Last edited by emare; 06-16-2016 at 09:02 AM..
# 4  
Old 06-16-2016
awk works linewise unless you'd redefine the record separator. Which you don't. So your try cannot work like posted. On top, you're talking of CRLF chars but there are none in the input sample posted.

Howsoever, try

Code:
awk '
/<FullPallet>/  {FP = 1}
/<AviExp>/      {FP = 0}
FP &&
/<BlockCode>/   {print "<AviExp>123456</AviExp>"}
1
' file
<PackDate>20160615</PackDate>
<TMCCode>BA</TMCCode>
<FullPallet>N</FullPallet>
<AviExp>123456</AviExp>
<BlockCode>0</BlockCode>
<AdmQualifier>568U#3-335</AdmQualifier>

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing different time formats

I am trying to do a comparison of files based on their last modified date. I am pulling the first file from a webapp folder using curl. curl --silent -I http://localhost:8023/conf/log4j2.xml | grep Last Last-Modified: Tue, 22 Mar 2016 22:02:18 GMT The second file is on local disk. stat... (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

2. Shell Programming and Scripting

How to compare dates in different formats?

Hi, I have a log file that records date time stamp in this format <Jun 7, 2015 12:56:54 PM EDT>. The date command give me the date in this format Mon Jun 8 08:40:58 EDT 2015 If the difference in the timestamp in the logfile & the current System datetime stamp is less than 15 mins then... (4 Replies)
Discussion started by: shifahim
4 Replies

3. Shell Programming and Scripting

Need to check the file formats

Hi, I want to check the incoming files whether the file is Mac file or dos/windows file in unix shell script. Sometimes client is posting Mac file and sometimes it is dos file. Could you please help me how to determine/check whether the file is Mac or dos. Help in advance Thanks (4 Replies)
Discussion started by: lkeswar
4 Replies

4. Shell Programming and Scripting

Output with horizontal formats

// AIX 5.3 & 6.1 This command powermt display dev=all returns the output of Pseudo name=hdiskpower50 Symmetrix ID=000190101757 Logical device ID=05F0 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ==============================================================================... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

5. Shell Programming and Scripting

Compare two files in different formats and get output

File A: DATAFILE TABLESPACE ------------------------- ------------------------- /dev/rprod_0032_011 D_EEM /dev/rprod_0032_012 D_ESO_REF ... (1 Reply)
Discussion started by: Daniel Gate
1 Replies

6. UNIX for Dummies Questions & Answers

Help with Date Formats

Hi, Following are the results of various date formats: 1. date +%h" "%d Result: Jun 02 2. date Result: Tue Jun 2 09:59:15 CDT 2009 If i use the date format as date +%h%d then i am getting the date as 02. I want the day to be displayed as "2" instead of "02". so my result should... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

7. Solaris

different date formats in same server

when I ssh and run date command, it shows date in 24 hour date format. But when I telnet the same server, it shows date in 12 hour format, ie. in AM/PM (1 Reply)
Discussion started by: na75369
1 Replies

8. UNIX for Dummies Questions & Answers

Change multiple filename formats with WHILE

Hi All, I'm trying to run a simple shell program to change all the files named *.cvs to *.txt. I am trying to use WHILE and this is what I have so far: This changes the first file from *.cvs to *.txt, but it is not cycling through the other files. My suspicion is that I don't have the... (5 Replies)
Discussion started by: ScKaSx
5 Replies

9. UNIX for Dummies Questions & Answers

date formats

Hi, I want to generate file name in the following date format, "YYYYMMDDHHHHMISS" plz. help me how to do that? (6 Replies)
Discussion started by: harshad.katruwa
6 Replies
Login or Register to Ask a Question