Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Sets of lines delimited by # to single lines
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 19th October 07:00
paul bullack
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines



Hello gurus, wizards; magicians, beginners,

how to solve this with sed (preferred) or awk

SOURCE (ASCII file)
=================
Line01
Line02
Line03
#
Line03
Line01
#
Line01
Line04
Line02
Line03
#
Line02
#

RESULT (ASCII file)
===================
Line01 Line02 Line03
Line03 Line01
Line01 Line04 Line02 Line03
Line02


Thanks for any response, Paul
  Reply With Quote


 


2 19th October 07:00
vassilis
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines



Try this out:

BEGIN { RS = "#\n" }

{
n = split($0, a, "\n")
for (i = 1; i < n; i++)
printf "%s ", a[i]
print a[i]
}
  Reply With Quote
3 19th October 07:00
gazelle
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


sed would be OT, so let's not go there.


BEGIN {RS="#"}
$1=$1
  Reply With Quote
4 19th October 07:00
paul bullack
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


How simple, problem solved

thanks to all

Paul

"Kenny McCormack" <gazelle@xmission.xmission.com> schrieb im Newsbeitrag
news:ec6o40$e6s$1@news.xmission.com...
  Reply With Quote
5 19th October 07:00
joojle
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


i wrote a sed one ,not good but it works

sed '/^$/d;:t;N;/#/!bt;s/#\|\n\n*/ /g'

regards
  Reply With Quote
6 19th October 07:00
mag gam
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


{
if ($1 != "#" )
{
printf ("%s ",$1);
}
else
{
printf ("\n");
}
}

hth
  Reply With Quote
7 19th October 07:00
gazelle
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


Error! Trailing space.
  Reply With Quote
8 19th October 07:00
mag gam
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


"data" is your datafile

/usr/bin/awk -f prog.awk data
-bash-3.00$ cat prog.awk
{
if ($1 != "#" )
{
printf ("%s ",$1);
}
else
{
printf ("\n");
}
}

hth
  Reply With Quote
9 19th October 07:00
jon labadie
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


^^^

Your program prints a space at the end of each line.
Not desirable in most sets of data.
  Reply With Quote
10 19th October 07:00
xicheng jia
External User
 
Posts: 1
Default Sets of lines delimited by # to single lines


In fact, all proposed solutions so far have some gotcha. besides the
leading/trailing space problems, it's also problematic with the record
rebuilding when there are TABs or adjacent whitespaces in a record.
here are some other solutions:

awk -v RS="\n#" '{ gsub(/\n/," "); gsub(/^ | $/,"") }1'

or

{
if ($0 ~ /^#/) {
SP = "\n";
} else {
printf("%s%s", SP, $0);
SP = " ";
}
}

or use two s/// commands in sed/perl:

sed -n 'H;$ba;/^#/{:a;x;s/\n/ /g;s/^#\? \| #\?$//g;p;}'
perl -lp043e 's/\n/ /g;s/^ | $//g'

Regards,
Xicheng
  Reply With Quote
Reply


Thread Tools
Display Modes




666