Concatenating lines ending with '+' using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating lines ending with '+' using awk
# 1  
Old 07-29-2010
Concatenating lines ending with '+' using awk

Hi,

I have an ASCII text file where some of the lines are ending with '+' character.

I have to concatenate the next successive line with those lines having the trailing '+' char by removing that char.

The below awk code has some problems to do this task:
Code:
awk '{while(sub(/\+$/,"")) { getline a; $0=$0 "\n" a; }; $0=$0 "\n"}'

Could anyone correct this code?
# 2  
Old 07-29-2010
Something like this?
Code:
awk '/\+$/{sub("\+"," ");printf $0;next}1' file

# 3  
Old 07-29-2010
sed ok?
Code:
sed -e :a -e '/\+$/N; s/\\\n//; ta'  infile > outfile

# 4  
Old 07-29-2010
Hi,

Try this
Code:
awk 'BEGIN {ORS=""} {if (/\+/) { gsub(/\+$/,"",$0); print ; getline; print"\n"} else { print $0"\n"}}'



---------- Post updated at 07:10 AM ---------- Previous update was at 06:48 AM ----------

Hi Franklin,

Could you please explain me the below

Code:
awk '/\+$/{sub("\+"," ");printf $0;next}1' file


Last edited by pravin27; 07-29-2010 at 07:57 AM..
# 5  
Old 07-29-2010
Code:
 /regex/ {code in here; next} 1

means if the regex is true execute the code { } inside the anonymous block
next means skip on to the next record.
the { } 1 - the trailing 1 - means else print $0, because 1 evaluates to true
# 6  
Old 07-29-2010
Thank you for all the posts.

But is it possible to tell how can I fix my code ?
Code:
awk '{while(sub(/\+$/,"")) { getline a; $0=$0 "\n" a; }; $0=$0 "\n"}'

# 7  
Old 07-29-2010
Quote:
Originally Posted by royalibrahim
Thank you for all the posts.

But is it possible to tell how can I fix my code ?
Code:
awk '{while(sub(/\+$/,"")) { getline a; $0=$0 "\n" a; }; $0=$0 "\n"}'

Should be something like this:
Code:
awk '{if(sub(/\+$/,"")){ getline a; $0=$0 " " a; }}{print}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating the lines of a data

I have a data of 1 lac lines with the following format abcde,1,2,3,4, ,ee ,ff ,gg ,hh ,mm abcde,3,4,5,6, ,we ,qw ,as ,zx ,cf abcde,1,5,6,7, ,dd ,aa ,er .... .... (6 Replies)
Discussion started by: aravindj80
6 Replies

2. Shell Programming and Scripting

Delete all lines ending with /

I have a text file with contents given below: file:///About/ file:///About/accessibility.html file:///About/disclaimer.html file:///About/disclaimer.html#disclaimer file:///About/glance/contact_info.html file:///books/ file:///bookshelf/br.fcgi?book=helppubmed&part=pubmedhelp... (6 Replies)
Discussion started by: shoaibjameel123
6 Replies

3. UNIX for Dummies Questions & Answers

how to remove lines ending with '*'

I have a file where some lines end with '*'. I would like to remove those lines ending with '*'. inFile: a b* c d*outFile: a cThank you (7 Replies)
Discussion started by: jdhahbi
7 Replies

4. Shell Programming and Scripting

Concatenating lines in bash

Hi, I'm attempting to join two lines in a file which are separated by a line break. The file contents are shown below: event_id=0 id=0_20100505210853 IFOconfig=HLV template=TaylorF2 Nlive=1000.0 Nruns=1.0 NIFO=3... (7 Replies)
Discussion started by: Supersymmetric
7 Replies

5. Shell Programming and Scripting

awk: switching lines and concatenating lines?

Hello, I have only recently begun with awk and need to write this: I have an input consisting of a couple of letters, a space and a number followed by various other characters: fiRcQ 9( ) klsRo 9( ) pause fiRcQ 9( ) pause klsRo continue 1 aPLnJ 62( ) fiRcQ continue 5 ... and so on I... (7 Replies)
Discussion started by: Borghal
7 Replies

6. Shell Programming and Scripting

Concatenating lines of separate files using awk or sed

For example: File 1: abc def ghi jkl mno pqr File 2: stu vwx yza bcd efg hij klm nop qrs I want the reult to be: abc def ghistu vwx yza jkl mno pqrbcd efg hij klm nop qrs (4 Replies)
Discussion started by: tamahomekarasu
4 Replies

7. Shell Programming and Scripting

Concatenating the lines with different pattern

Hi, I have put a similar question in one of the other threads through which I got the solution shown below but I have some more condition to add to it, hence have further queries on it. I appologies if I should be putting this with the old thread. I have a file which perform a grep on the... (1 Reply)
Discussion started by: simi28
1 Replies

8. Shell Programming and Scripting

Searching for lines ending with }

I'm trying to search for lines ending with "}" with the following command but am not getting any output. grep '\}$' myFile.txt I actually want to negate this (i.e. lines not ending with "}"), but I guess that should be easier once I find the command that finds it? (11 Replies)
Discussion started by: BootComp
11 Replies

9. Shell Programming and Scripting

Concatenating the two lines in a file

hi My requirement is i have a file with some records like this file name ::xyz a=1 b=100,200 ,300,400 ,500,600 c=700,800 d=900 i want to change my file a=1 b=100,200,300,400 c=700,800 d=900 if record starts with " , " that line should fallows the previous line.please give... (6 Replies)
Discussion started by: srivsn
6 Replies

10. Shell Programming and Scripting

Concatenating lines and formatting.

Hi, I have a html file which is unformatted and need to concatenate the lines between each "table" statement in order to run an awk statement on it. Here is the example of the raw file: <table border="0" cellspacing="0" cellpadding="0" class="playerDetails"> def ... (3 Replies)
Discussion started by: Tonka52
3 Replies
Login or Register to Ask a Question