Append Next line with current Line bassed on condition

Hi,

I have an XML file and I am tring to extract some data form it, after lot of data cleaning process, I ended up with an issue, and need your urgent support.

my current input data in below format:

<Node>xxxxxx
 <Node>yyyyy</Node>
 <Node>zzzzzz
 <Node>12345</node>

I need your upmost support to get the data in below format:

<Node>xxxxxx<Node>yyyyy</Node>
 <Node>zzzzzz<Node>12345</node>

I tried with several option in this forum but it's not giving proper output.

Hello rramkrishnas,

Using words like "urgent" are prohibited to be used in normal forums. You could post your question in "emergency unix and linux support" forum(http://www.unix.com/emergency-unix-and-linux-support/\). Posting a new thread to this forum requires Bits. We monitor this forum to help people with emergencies, but we do not guarantee response time or answers. This forum is "best effort" only. Also it is a good practice to show us what have you tried with OS details etc.

Thanks,
R. Singh

I regret for the inappropriate words used in my post.
I am using CYGWIN to perform this task. I searched thru this forum to fix my issue, and found many useful codes, but all the outputs are placing current line appending with previous line.
however my requirement is to append the next line with current line.

---------- Post updated at 12:48 PM ---------- Previous update was at 12:38 PM ----------

I tried below code, but ending up with getting curent line appending to previous line.

awk '!/\>$/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}'  2.txt > 3.txt

Hello rramkrishnas,

Could you please try following and let me know if this helps you.
Let's say following is the Input_file:

cat Input_file
<Node>xxxxxx
<Node>yyyyy</Node>
<Node>zzzzzz
<Node>12345</node>
 

Following is the code then.

awk '{ORS=NR%2==0?"\n":""}1'  Input_file

Output will be as follows.

<Node>xxxxxx<Node>yyyyy</Node>
<Node>zzzzzz<Node>12345</node>
 

Thanks,
R. Singh

1 Like

If you are just trying to join pairs of lines keeping the initial spacing on odd numbered lines unmodified and removing leading spaces on even numbered lines, try:

awk '
NR % 2{	printf("%s", $0)
	next
}
{	print $1
}' 2.txt > 3.txt

If you are trying this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .

Producing the same output with code closer to what you had, you could try:

awk '!/>$/{if(x)print x;x="";}{x=(!x)?$0:x $1}END{print x}' 2.txt > 3.txt
$ awk '!/\>$/ { getline nxtLne; $0 = $0 nxtLne } 1 ' file
<Node>xxxxxx <Node>yyyyy</Node>
 <Node>zzzzzz <Node>12345</node>

Hi Ravindra,

Thanks you very much for your much needed help, however regret to bother you again. I find that the data was present in below format

 <Node>xxxxxx
<Node>yyyyy</Node>
<Node>zzzzzz
<Node>AAAAAAAAA
 <Node>12345</node>
 

and the output I need in the format:

 <Node>xxxxxx<Node>yyyyy</Node>
<Node>zzzzzz<Node>AAAAAAAAA<Node>12345</node>
 

Hello rramkrishnas,

I am not sure the output you have shown which shows us leading spaces are uneven, so if you need exact output as you have shown us then following may be helpful to you.

awk '{if($0 ~ />$/){sub(/^[[:space:]]+/,X,$0);ORS="\n"} else {ORS=""}} 1'  Input_file

Output will be as follows.

 <Node>xxxxxx<Node>yyyyy</Node>
<Node>zzzzzz<Node>AAAAAAAAA<Node>12345</node>

If in case you don't want to have any leading spaces then following may help you in same.

awk '{sub(/^[[:space:]]+/,X,$0);ORS=$0 ~ />$/?"\n":""} 1'  Input_file

Output will be as follows.

<Node>xxxxxx<Node>yyyyy</Node>
<Node>zzzzzz<Node>AAAAAAAAA<Node>12345</node>

Let me know if you have any queries.

Thanks,
R. Singh

1 Like

Thanks Ravindra,

It has worked perfectly and I am going to have good time now.

This will help me to save lot of time in converting my data.

Regards,
Ram

Try also

awk '{ORS=/\/[nN]ode/?"\n":""}1' file
1 Like