[Solved] Line Break Issue for an XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Line Break Issue for an XML file
# 1  
Old 10-18-2011
[Solved] Line Break Issue for an XML file

I got an XML file(file name TABLE.xml) which the data format has line breaks(with no Spaces, no Nulls, no characters between each line), I need to write a KSH script which gives me the data in single line as format shown below

My input file which have line breaks:
Code:
<TABLE>
<TABLE-ROW> 
<S_NO> 
1 
</ S_NO > 
<REP_TYPE> 
TEST1 
</REP_TYPE> 
<PROCESS_OFFICE> 
P 
</PROCESS_OFFICE> 
<PACKAGE_LEVEL> 
C 
</PACKAGE_LEVEL> 
</TABLE-ROW> 
<TABLE-ROW> 
<S_NO> 
2 
</ S_NO > 
<REP_TYPE> 
TEST2 
</REP_TYPE> 
<PROCESS_OFFICE> 
L 
</PROCESS_OFFICE> 
<PACKAGE_LEVEL> 
C 
</PACKAGE_LEVEL> 
</TABLE-ROW> 
<TABLE-ROW> 
<S_NO> 
3 
</ S_NO > 
<REP_TYPE> 
TEST3 
</REP_TYPE> 
<PROCESS_OFFICE> 
L 
</PROCESS_OFFICE> 
<PACKAGE_LEVEL> 
C 
</PACKAGE_LEVEL> 
</TABLE-ROW> 
</TABLE>

Expected output format for the above table should be like shown below:
Code:
<TABLE>
<TABLE-ROW> 
<S_NO>1</ S_NO > 
<REP_TYPE>TEST1</REP_TYPE> 
<PROCESS_OFFICE>P</PROCESS_OFFICE> 
<PACKAGE_LEVEL>C</PACKAGE_LEVEL> 
</TABLE-ROW> 
<TABLE-ROW> 
<S_NO>2</ S_NO > 
<REP_TYPE>TEST2</REP_TYPE> 
<PROCESS_OFFICE>L</PROCESS_OFFICE> 
<PACKAGE_LEVEL>C</PACKAGE_LEVEL> 
</TABLE-ROW> 
<TABLE-ROW> 
<S_NO>3</ S_NO > 
<REP_TYPE>TEST3</REP_TYPE> 
<PROCESS_OFFICE>L</PROCESS_OFFICE> 
<PACKAGE_LEVEL>C</PACKAGE_LEVEL> 
</TABLE-ROW> 
</TABLE>


Last edited by Franklin52; 10-19-2011 at 03:58 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-18-2011
This should work in most shells:

Code:
$ cat xmlline.sh

#!/bin/sh

while read LINE
do
        if [ "${LINE:0:1}" != "<" ]
        then
                DATA="${DATA}${LINE}"
                continue
        fi

        if [ "${LINE:0:2}" = "</" ]      # close-tags
        then
                if [ -z "$DATA" ]
                then
                        printf "\n%s" $LINE
                else
                        printf "%s" "${DATA}${LINE}"
                        DATA=""
                fi
        else
                # Add a tag
                printf "\n%s" $LINE
        fi
done

echo    # Print the last newline

$ ./xmlline.sh < file.xml

<TABLE>
<TABLE-ROW>
<S_NO>1</ S_NO >
<REP_TYPE>TEST1</REP_TYPE>
<PROCESS_OFFICE>P</PROCESS_OFFICE>
<PACKAGE_LEVEL>C</PACKAGE_LEVEL>
</TABLE-ROW>
<TABLE-ROW>
<S_NO>2</ S_NO >
<REP_TYPE>TEST2</REP_TYPE>
<PROCESS_OFFICE>L</PROCESS_OFFICE>
<PACKAGE_LEVEL>C</PACKAGE_LEVEL>
</TABLE-ROW>
<TABLE-ROW>
<S_NO>3</ S_NO >
<REP_TYPE>TEST3</REP_TYPE>
<PROCESS_OFFICE>L</PROCESS_OFFICE>
<PACKAGE_LEVEL>C</PACKAGE_LEVEL>
</TABLE-ROW>
</TABLE>

$

It's a very straightforward parser and doesn't check for improperly nested tags, etc.

It was a very small modification to ditch the $1,$2,... array entirely since it's not doing checking.

Last edited by Corona688; 10-18-2011 at 05:48 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-18-2011
Thank you for the fast response.
This shell script is Printing the output which is giving correct format which I wanted, but I need to save the output as a another file in another directory
# 4  
Old 10-18-2011
If you can print it to your terminal, you can save it anywhere.

Code:
./xmlline.sh < input > /path/to/output

# 5  
Old 10-18-2011
one line awk. Smilie
Code:
awk '{printf /^</&&!/^<\//?RS $0:$0}' infile

# 6  
Old 10-19-2011
How does it work?
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-19-2011
Corona, It worked, Thank you very much.
I appreciate.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

[SOLVED] Grep IP's from xml with name of file

Hi all, actually i have a lot of IP addresses on multiple .xml files on my folder, my tag on xml files are like this, <db-url>jdbc:oracle:thin:@10.100.5.56:1521:DWH01</db-url> i actually can print only the IP addresses awk -F"" '/url/ { print $3 }' *.xml 10.100.5.56 i would like to grep... (2 Replies)
Discussion started by: charli1
2 Replies

2. Shell Programming and Scripting

Line Break Issue in XML file

Hi Experts, Kindly help me to resole the line break issue mentioned below sample xml file: sample source File: ============== <?!New XML File><NEWRECORDS xmlns... (3 Replies)
Discussion started by: UnniVKN
3 Replies

3. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

4. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

5. Shell Programming and Scripting

Add line break for each line in a file

I cannot seem to get this to work.. I have a file which has about 100 lines, and there is no end of line (line break \n) at the end of each line, and this is causing problem when i paste them into an application. the file looks like this this is a test that is a test balblblablblhblbha... (1 Reply)
Discussion started by: fedora
1 Replies

6. Shell Programming and Scripting

How to remove line break character in a file

Hi, we are trying to process a csv file,in which we are getting data with line breaks.How to remove the line break character in the file? when i try to print the line break charcter using od -c,it gives as '\n' character for both line break and line feed. Please provide your valuable... (6 Replies)
Discussion started by: cnraja
6 Replies

7. Shell Programming and Scripting

How to remove line break in a csv file

Hi Experts, My requirement is to read the csv file and need to remove if any line break in it. sample data: Row1: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 Row2:"Tønsberg, Brygga Kino SF",Tønsberg,202-1, Tønsberg SF 4,202-1-4 Expected data: Row1: "Oslo, Symra... (6 Replies)
Discussion started by: cnraja
6 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

10. Shell Programming and Scripting

Replacing characters in file with line break

Hi, Apologies if this has been asked before, but I searched and was not able to find an answer. It's probably a simple question to answer for those of you with some experience, though... I have a relatively long string where tokens are separated by the colon (':') character. Let's say the... (10 Replies)
Discussion started by: johnemb
10 Replies
Login or Register to Ask a Question