Concatenating lines ending with '+' using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating lines ending with '+' using awk
# 8  
Old 07-29-2010
Code:
gawk 1 RS='+\n' ORS='' file

# 9  
Old 07-30-2010
Quote:
Originally Posted by Franklin52
Should be something like this:
Code:
awk '{if(sub(/\+$/,"")){ getline a; $0=$0 " " a; }}{print}' file

good one, but it does not work when the consecutive lines have '+'. Also, can you please explain the logic behind this, if possible?
# 10  
Old 07-30-2010
Quote:
Originally Posted by royalibrahim
good one, but it does not work when the consecutive lines have '+'. Also, can you please explain the logic behind this, if possible?
In that case you can use a while loop:
Code:
awk '{while(sub(/\+$/,"")){ getline a; $0=$0 " " a; }}{print}' file

# 11  
Old 07-30-2010
[code] #!/bin/bash declare -i flag flag=0 while read -r LINE do case "$LINE" in *+) LINE="${LINE%+*}" printf "%s " $LINE flag=1 continue esac [[ $flag == 1 ]] && echo "$LINE" && flag=0 [[ $flag == 0 ]] && echo "$LINE" done

---------- Post updated at 05:15 AM ---------- Previous update was at 05:12 AM ----------

Code:
#!/bin/bash

declare -i flag
flag=0
while read -r LINE
do
  case "$LINE" in
   *+)
    LINE="${LINE%+*}"
    printf "%s " $LINE
    flag=1
    continue
  esac
  [[ $flag == 1 ]] && echo "$LINE" && flag=0
  [[ $flag == 0 ]] && echo "$LINE"
done <"myfile"

# 12  
Old 08-02-2010
Quote:
Originally Posted by Franklin52
In that case you can use a while loop:
Code:
awk '{while(sub(/\+$/,"")){ getline a; $0=$0 " " a; }}{print}' file

Awesome!! could you please explain me the logic??
What is the difference between 'getline' and 'next' in awk?

Last edited by royalibrahim; 08-02-2010 at 03:59 AM..
# 13  
Old 08-02-2010
Quote:
Originally Posted by royalibrahim
Awesome!! could you please explain me the logic??
If a line ends with a "+" sign the sub function returns a 1. In that case we get the next line, store it in the variable a, appends it to the previous line ($0) and try to substitute the next "+" ...and so on.

Quote:
Originally Posted by royalibrahim
What is the difference between 'getline' and 'next' in awk?
After a getline command, awk continues with the next statement after the getline command but after a next command awk continues with the first statement of the awk program.
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