Hi,
I have learned some of the Unix commands a way back and not sure of how to code them when needed in certain way, especially sed command. Here is my situation. I have an xml file with several tags. most of the tags start on the same line and end on the same line. However, data for some tags span into mulitple lines. I would like to bring back that particular tag into one line removing all new lines between them.
Here is an example:
<Report>
<Project>
<Proj_Name>ABC Enhancement</Proj_Name>
<Proj_Type>Mechanical</Proj_Type>
<Proj_Description>Project started on 01/03/2006.
However, it is running behind due to unavailable
Resources</Proj_Description>
<Proj_Hours>123.00</Proj_Hours.
</Project>
<Report>
The above is a sample data. I am looking to remove new line characters only from the lines that spans into multiple lines. Herea is how it should appear after removing new lines.
<Report>
<Project>
<Proj_Name>ABC Enhancement</Proj_Name>
<Proj_Type>Mechanical</Proj_Type>
<Proj_Description>Project started on so and so date.... </Proj_Description>
<Proj_Hours>123.00</Proj_Hours.
</Project>
<Report>
Hi,
Thanks for your quick reply. First thing I noticed is that this script is taking little long time to process my file(size 200mb). I have about 10 to 20 files to process like this and I am afraid it might take several minutes process. Secondly, I thouhgt, if we remove extra new line characters, the size of the result file should be less than or equal to the original size, but I see the resulted file size being increased than original size. Thirdly, can you please explain me on your command what exactly is each command doing?
Also, I noticed that the above script provided was not working, though executing successfully, to remove newline characters between a pair of html tags.
Hi,
As I am new to Perl scripting, I appologize for my dumb questions. Please help me understand it so that I can utilize it. I added a line to it to open a file, I read this command some where in the net, but not sure where are we writing re-formatted input lines back. Here is it how it looks now.
#!/usr/local/bin/perl
open (MYFILE, "/ABC/XYZ/A123/XmlFldr/test1.xml") or die("Unable to open File");
while (<MYFILE>) {
chomp;
if ( /.*\>$/ ) {
print "$\n";
} else {
print "$";
}
}
Also, I noticed that in my input data, especially when a particular tag has data span into multiple lines, for ex:
<Proj_Name>ABC - Mechanical fix<Proj_Name>
<Proj_Descritpion> ABC - Mechanical fix
to a generator x1234m. <Proj_Description>
<Proj_Comment>Project started on so and so date.
It is now running behind schedule due to
unavailable resources<Proj_Comment>
In the above case there are two tags that has data split into multiple lines. This data file comes from a windows environment. So, I am not sure if it has both newline and CR breaking the line.
With Awk script indicated above, it did not convert these lines into one line tags. however, it appears to be working fine by putting remaining lines into one line tags. I would like to get the multiline data tags into one single line.
you need to add nothing to the file
tst.pl tst.txt
should produce the output if you use my original script and txt file
I thought I edited my original post to include the command line, but I must not have saved it.
#!/usr/bin/env perl
while (<>) { #treat parameters as filenames and open them for reading (in this case you should only process one file at a time)
chomp; # remove last character if it is a linefeed
if ( /.*\>$/ ) { # look for any character (the .) repeat as often as possible (the *) followed by a '>' character (the \>) but only at the end of the line (the $). So 'zbc> ' does not match, since the > character is not at the end of the line.
print "$\n";
} else {
print "$";
}
}
Scott,
Thanks for your kind explanation and help on the script you provided. I now understand the script and ran it. However, for somereason, I noticed that it is working only when I opened my data file and make any change and save it back and then run the script you provided. In such case it produced the result like this, which is what I wanted.
It does not seem to be inserting the new line character properly, with each line. I am not sure why it is behaving like this. Because we are getting the data file produced from windows server and sent it via ftp to Lynux box, the file may have any other character to represent linefeed?
You could also try the following once you get the file on unix
perl -pi -e 's/\r\n/\n/;' <filename>
This last command is from dos2unix - perl and its wonderful tricks