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:
awk '{while(sub(/\+$/,"")) { getline a; $0=$0 "\n" a; }; $0=$0 "\n"}'
Could anyone correct this code?
Something like this?
awk '/\+$/{sub("\+"," ");printf $0;next}1' file
sed ok?
sed -e :a -e '/\+$/N; s/\\\n//; ta' infile > outfile
Hi,
Try this
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
awk '/\+$/{sub("\+"," ");printf $0;next}1' file
/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
Thank you for all the posts.
But is it possible to tell how can I fix my code ?
awk '{while(sub(/\+$/,"")) { getline a; $0=$0 "\n" a; }; $0=$0 "\n"}'
royalibrahim:
Thank you for all the posts.
But is it possible to tell how can I fix my code ?
awk '{while(sub(/\+$/,"")) { getline a; $0=$0 "\n" a; }; $0=$0 "\n"}'
Should be something like this:
awk '{if(sub(/\+$/,"")){ getline a; $0=$0 " " a; }}{print}' file
binlib
July 29, 2010, 9:53am
8
gawk 1 RS='+\n' ORS='' file
good one, but it does not work when the consecutive lines have '+'. Also, can you please explain the logic behind this, if possible?
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:
awk '{while(sub(/\+$/,"")){ getline a; $0=$0 " " a; }}{print}' file
kurumi
July 30, 2010, 6:15am
11
#!/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 ----------
#!/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"
franklin52:
In that case you can use a while loop:
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?
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.
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.