want to skip a line in XML file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want to skip a line in XML file using awk
# 15  
Old 09-03-2012
HI People i used your script and i am getting the first file without any issue. but second xml file i am getting first two lines tag. you can see the abouve our pt i pasted.... thee you can see <xml version 1.0> tag and <notification> tag occuring in second and third file...
# 16  
Old 09-04-2012
Unable to eliminate a line in my file using awk plz help

Moderator's Comments:
Mod Comment Threads merged.


I have multiple xml files. each xml file has multiple blocks. Now what i have to do is split all the blocks in each xml file and append it to a main xml file. while spliting each xml file i have to eliminate three lines. sadly i was unable to do that. Any awk expert here plz help meSmilie .
Below is two sample xml files.
Code:
file1.xml
------------
<?xml version="1.0"?>
<notification>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12222</alarmId>
</alarmNew>
</notification>

file2.xml
-----------
<?xml version="1.0"?>
<notification>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>
</notification>

Below is the code i am using which i got from experts here:
Code:
#!/bin/awk -f
BEGIN {
   AlarmNbeg  = "alarmNew";
   AlarmClbeg = "alarmCleared";
   AlarmChbeg = "alarmChanged";
   AckStatbeg = "ackStateChanged";
   TotBlocks = 0;
}
!/^(<\?xml version="1\.0"\?>|<\/?notification>)$/ {
   if((length($0) > 1)) {

      if((substr($0, 2, length(AlarmNbeg)) ==  AlarmNbeg)|| (substr($0, 2, length(AlarmClbeg)) ==  AlarmClbeg) || (substr($0, 2, length(AlarmChbeg)) ==  Alar
mChbeg) || (substr($0, 2, length(AckStatbeg)) ==  AckStatbeg)) {
         print "<?xml version=\"1.0\"?>\n";
          printf $0;
          printf "\n";
      }
else {
       printf $0;
      printf "\n";
     }
   }
}
END {
}

The output i am getting is:
Code:
o/p coming:

<?xml version="1.0"?>
<notification>
<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12222</alarmId>
</alarmNew>

<?xml version="1.0"?>
<notification>
<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

Expected output is:
Code:
<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12222</alarmId>
</alarmNew>

<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

some one plz help.

Last edited by Corona688; 09-04-2012 at 01:49 PM..
# 17  
Old 09-04-2012
Code:
$ cat xmlcat.awk

BEGIN {
        RS=""           # Split records on blank lines
        FS=OFS="\n"     # Each field is a different line
        ORS="\n\n"      # Output blank lines, too
}

# Add missing XML lines
!/<[?]xml/{     $0="<?xml version=\"1.0\"?>\n" $0;      }

{       # Loop over lines
        for(N=2; N<=NF; N++)
        if(!(($N ~ /<alarmId>/)||($N ~ /<[/]?alarmNew/))) $N="";

        gsub(/\n+/, "\n");
        sub(/\n$/, "");
} 1

$ awk -f xmlcat.awk data1 data2

<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12222</alarmId>
</alarmNew>

<?xml version="1.0"?>
<alarmNew systemDN="GGSN-testmy11111111">
<alarmId>12528</alarmId>
</alarmNew>

$

Use nawk on solaris.
This User Gave Thanks to Corona688 For This Post:
# 18  
Old 09-04-2012
Isn't this the same issue as your other thread?
This User Gave Thanks to CarloM For This Post:
# 19  
Old 09-04-2012
HI COrona688,
thanks for this i will try in my environment, i have small doubt can i run the above command as " nawk-f xmlcat.awk *.xml" because i dont know how many xml files will flow in that folder. so if i mention file name as foldername/*.xml. will this works for all xml files in that folder????

HI CarloM,
yes u r right but i got no response thereSmilie
# 20  
Old 09-04-2012
Yes, thoug you need a space between nawk and -f of course. * should work for all xml files in a folder. Expanding * into multiple filenames is a property of the shell, not of awk, so it should work for most things really.

The only gotcha is that if you have thousands and thousands of them, * will run out of room to cram arguments, since you can only put so many in one line.

Last edited by Corona688; 09-04-2012 at 01:51 PM..
This User Gave Thanks to Corona688 For This Post:
# 21  
Old 09-05-2012
Thanks, i am sure the xml file count wont cross more than 500 files maximum. so * should work.

---------- Post updated at 03:07 PM ---------- Previous update was at 02:27 PM ----------

HI ,
I executed the script and below is the error what i am getting. plz tell me what is missing.
Code:
nawk -f Align1.awk ./foldername/*.xml
nawk: nonterminated character class <[
 source line number 12
 context is
                if(!(($N ~ /<alarmId>/)||($N ~ >>>  /<[/ <<< ]?alarmNew/))) $N="";

More over you have gave me code to handle blocks with root element <alarmnew> </alarmnew> . I am having two more root elements <alarmchanged></alarmchanged> and <alarmack></alarmack> and child element is not only <alarmId> there may be many,it may increase or decrease, only root element is constant 3 elements which i mentioned in above lines.

Last edited by ganesan kulasek; 09-05-2012 at 06:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to skip record in file

I need to amend the code blow such that it reads a "black list" before the "print" statement; if "substr($1,1,6)" is found in the "blacklist" it will ignore that record and continue. the code is from an awk script that is being called from shell script which passes the input values. BEGIN { "date... (5 Replies)
Discussion started by: bazel
5 Replies

2. UNIX for Advanced & Expert Users

Read file and skip the line starting with #

Hi all, I'm new in unix. Need some help here. I have a file called server.cfg which contains the servers name, if I don't want to run on that server, I'll put a "#" infront it. username1@hostname.com username2@hostname.com #username3@hostname.com #username4@hostname.com... (17 Replies)
Discussion started by: beezy
17 Replies

3. Shell Programming and Scripting

Skip first and last line

Hi All I have a sample file like below: 012312112 1372422843 1236712 1372422843 1275127 3109301010 from which I wan't to: 1.)delete... (10 Replies)
Discussion started by: swasid
10 Replies

4. UNIX for Dummies Questions & Answers

skip first line when doing a read of csv file

Folks, how do i skip the first line in a csv, while doing the read of a csv file in to a variable line by line. eg : do echo $line done < $rpt where rpt is path to csv file The initial 1st line is a garbage that i want to avoid, and start reading from 2nd line ... (2 Replies)
Discussion started by: venu
2 Replies

5. Shell Programming and Scripting

Need help in using sed/awk for line insertion in xml

Hello, I have two text files (txt1 and txt2). txt1 contains many lines with a single number in each line. txt2 (xml format) contains information about the numbers given in txt1. I need to insert one line in txt2 within the scope of each number taken from txt1. Sample problem: txt1: 12 23... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

6. Shell Programming and Scripting

How to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. The name should be set to the date, which is a part of the following line of the xml file: <sceneID>C82_N32_A_SM_strip_008_R_2009-11-24T04:22:12.790028Z</sceneID> How can I separate this line, that the name will... (6 Replies)
Discussion started by: friend
6 Replies

7. Shell Programming and Scripting

how to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. My code looks like this: set name = `awk '/<generationTime>/,/<\/generationTime>/ p' $xml_name` the "name" is thus set to <generationTime>2004-12-01T08:23:50.000000</generationTime> How can I separate this line,... (3 Replies)
Discussion started by: friend
3 Replies

8. UNIX for Dummies Questions & Answers

How to skip first line from a file while manupulating the file

I need to put single quotes on the columns of a .csv file. The first row contains the column headers. I need to skip the first row and put quotes for rest of the rows. Would please someone help me with this. Thanks JP (4 Replies)
Discussion started by: JPalt
4 Replies

9. Shell Programming and Scripting

AWK to skip comments in XML file

Hello, I'm trying to make a shell script to skip comments from an XML file, but with the code below only deletes comments that are in one line. Can you tell me what can be added here? nawk ' { if($0 !~/<!--/) { a=0 } if($0 ~/<!--/ && $0 ~/-->/) {a=1} if($0 ~/<!--/) {a=1} if... (1 Reply)
Discussion started by: majormark
1 Replies

10. Shell Programming and Scripting

Skip new line

Hi, how can I skip the new line of echo? In SH!!!! echo "the date is :" date and result I want is the date is : Tue Oct 11 22:24:37 WEST 2005 I've already tried including the \c inside the echo, but it didn't work. Thanks! (2 Replies)
Discussion started by: pmpx
2 Replies
Login or Register to Ask a Question