Append Next line with current Line bassed on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append Next line with current Line bassed on condition
# 1  
Old 01-14-2016
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:

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

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

Code:
<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.
# 2  
Old 01-14-2016
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(https://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
# 3  
Old 01-14-2016
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.

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

# 4  
Old 01-14-2016
Hello rramkrishnas,

Could you please try following and let me know if this helps you.
Let's say following is the Input_file:
Code:
cat Input_file
<Node>xxxxxx
<Node>yyyyy</Node>
<Node>zzzzzz
<Node>12345</node>

Following is the code then.
Code:
awk '{ORS=NR%2==0?"\n":""}1'  Input_file

Output will be as follows.
Code:
<Node>xxxxxx<Node>yyyyy</Node>
<Node>zzzzzz<Node>12345</node>

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-14-2016
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:
Code:
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:
Code:
awk '!/>$/{if(x)print x;x="";}{x=(!x)?$0:x $1}END{print x}' 2.txt > 3.txt

# 6  
Old 01-14-2016
Code:
$ awk '!/\>$/ { getline nxtLne; $0 = $0 nxtLne } 1 ' file
<Node>xxxxxx <Node>yyyyy</Node>
 <Node>zzzzzz <Node>12345</node>

# 7  
Old 01-14-2016
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

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

and the output I need in the format:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

4. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

5. Shell Programming and Scripting

find a certain line and append text to the end of the line

After I create printer queues in AIX, I have to append a filter file location within that printers custom file. within lets say test_queue.txt I need to find the row that starts with :699 and then I need to append on the end the string /usr/local/bin/k_portrait.sh. Now I've gotten the sed... (2 Replies)
Discussion started by: peachclift
2 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

9. Shell Programming and Scripting

Vi Editor - How to paste the line concatenated with current line

I generally use yy to copy a line and then p to paste the line at end of current line. But is there a way to paste the copied line in concatenation with the current line with out going to next line. (3 Replies)
Discussion started by: paragkalra
3 Replies

10. Shell Programming and Scripting

Joining lines in reverse. append line 1 to line 2.

Hi I have used many times the various methods to append two lines together in a file. This time I want to append the 1st line to the second and repeat for the complete file.... an example This is the file owns the big brown dog joe owns the small black dog jim What I want is ... (7 Replies)
Discussion started by: dwalley
7 Replies
Login or Register to Ask a Question