Operating System - HP-UX
1832669 Members
2978 Online
110043 Solutions
New Discussion

conditional joining of lines

 
Zina
New Member

conditional joining of lines

basically id like to delete every occurance of "\n " in a text file{newline FOLLOWED by space at the begining of the next line} or {if a line start with space,delete the space and join it with the preceding one}

the following lines didnt work

awk '{if ($1 == NULL) {print X$0} else X=$0} END {print X}' tmp/search1.tmp > tmp/search111.tmp

sed 's/"\n "//g' tmp/search1.tmp > tmp/search111.tmp

this deletes evey \n and every space !
6 REPLIES 6
Eric SAUBIGNAC
Honored Contributor

Re: conditional joining of lines

Hi Zina, try this

#----
{
head -1 toto
tail +2 toto | sed 's/^ /#TO_BE_JOIN# /' | awk '
{
if (NR == 1) LINE=$0
else
if ($1 == "#TO_BE_JOIN#") LINE=LINE $0
else { print LINE; LINE=$0 }
}
END { print LINE }
'
} | sed 's/#TO_BE_JOIN# //g'
#----

And my file 'toto' is :

hello
Line 2 no Join
Line 3 to be join with 1 remaining space
Line 4 to be join
end

The result is :

hello
Line 2 no Join Line 3 to be join with 1 remaining spaceLine 4 to be join
end

If file toto is :

hello
Line 2 no Join
Line 3 to be join with 1 remaining space
Line 4 to be join
end

then result is :

hello
Line 2 no Join Line 3 to be join with 1 remaining spaceLine 4 to be joinend

I think there is no side effect ...

Hope this will help

Eric

(PBFWME;-)
Eric SAUBIGNAC
Honored Contributor

Re: conditional joining of lines

Zina,

i do not see in my previous answer some spaces i include in the code. In particulary
there a space folowing the tag '#TO_BE_JOIN#' in the sed commands

So here is an attachement where U will find the script as it shoul be

Regards

Eric
Orhan Biyiklioglu
Respected Contributor

Re: conditional joining of lines

Try this:

#awk '{ if (match($0, /^ /)) {print X$0; X=NULL} else{ if(X != NULL)print X; X=$0}}'

#cat joinme.txt
aaaa
bbb
ccc
ddd
>>>eee
fff
ggg
hhh
>>>iii

where >>> designates a space


# awk '{ if (match($0, /^ /)) {print X$0; X=NULL} else{ if(X != NULL)print X; X=$0}}' joinme.txt
aaaa bbb
ccc
ddd eee
fff
ggg
hhh iii

hth
James R. Ferguson
Acclaimed Contributor

Re: conditional joining of lines

Hi:

You could use this small perl script:

#!/usr/bin/perl
open( FH, "<", $ARGV[0] ) or die "Can't open $!\n";
$/ = undef;
$_ = ;
$_ =~ s%\n % %g;
print $_;

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: conditional joining of lines


'nother perl quicky:

perl -pe 'chomp; print "\n" unless /^ \S/' x.txt

The -p tells perl to loop through the input, put it in default var $_ and print $_ at then end of the block.
In the block we start by taking of the newline.
The newline is printed explicitly unless the line starts with a space.

An other perl way to express this, also taking care of the last line:

perl -pe 'chomp; print "\n" if (eof or /^[^ ]/)' x.txt

In awk you would need to call printf to avoid havinng print 'help' you with new-lines.

Hein.
Sandman!
Honored Contributor

Re: conditional joining of lines

Attached is code for a small C program that does what you want. Just compile and run it.

cheers!