ashok13123 has asked for the wisdom of the Perl Monks concerning the following question:

I am having a file like this
Firstline secondline Firstline secondline thirdline firstline
i want to append the lines which are starting with space to the previous line so that the file is like
Firstlinesecondline Firstlinesecondlinethirdline firstline
can any one help but the problem is if there is if it is like this
firstline54324563213612531436436463246324634 secondlidfndsfdsfdsfd thirdlinedsfsdf
then the o/p is like
firstline54324563213612531436436463246324634secondlidfndsfdsfdsfd thirdlinedsfsdf
but i want it in one line like
firstline54324563213612531436436463246324634secondlidfndsfdsfdsfdthird +linedsfsdf
is it possible?

Replies are listed 'Best First'.
Re: Appending lines starting with white space to the previous line
by 1Nf3 (Pilgrim) on Jan 07, 2009 at 07:16 UTC

    Try using  <code> </code> blocks to show your file contents.

    As to the question, i would try using regular expressions in multiline mode and replacing the newline if it's followed by whitespace with a single space character.

    Update:

    This should work:
    #!/usr/bin/perl -w use strict; $/ = undef; my $filecontents = <>; $filecontents =~ s/\n\s+//gm; print $filecontents;

    Regards, Luke

Re: Appending lines starting with white space to the previous line
by ikegami (Patriarch) on Jan 07, 2009 at 08:51 UTC

    Re: Appending lines starting with white space to the previous line reads the entire file into memory. This version processes the file line by line thanks to redo and a one line lookahead.

    chomp( my $line = <$fh> ); while (defined($line)) { chomp( my $next = <$fh> ); if (defined($next) && $next =~ s/^\s+//) { $line .= $next; redo; } ... do something with $line ... $line = $next; }

    Update: Tested. Small syntax fix.

Re: Appending lines starting with white space to the previous line
by imrags (Monk) on Jan 07, 2009 at 08:16 UTC
    TIMTOWTDI
    my $i=0; my $temp = "ARR"; while (<FILE>) { if ($_ !~ /^\s+/) { chomp; $i++; @{$temp{$i}} = ($_); } else { my $temp2 = $_; chomp ($temp2); $temp2 =~ s/^\s+//g; push (@{$temp{$i}},$temp2); } } close (FILE); for $j(0..$i) { print "@{$temp{$j}}"."\n"; }
    Raghu
Re: Appending lines starting with white space to the previous line
by linuxer (Curate) on Jan 07, 2009 at 09:31 UTC

    Hi,

    this code parses the input linewise and prints directly to STDOUT, so you can redirect the result to another file.

    while ( my $line = <DATA> ) { chomp $line; # remove the spaces from start of line if ( $line =~ s/^\s+// ) { # print line if successful replacement print $line; } else { # print newline (not for first line) print "\n" if $. != 1; print $line; } } __DATA__ first second third first second third first second first

    Update:

    shorter version:

    while ( my $line = <DATA> ) { chomp $line; # remove the spaces from start of line if ( ! ( $line =~ s/^\s+// ) ) { # print newline (not for first line) print "\n" if $. != 1; } print $line; }
Re: Appending lines starting with white space to the previous line
by sathiya.sw (Monk) on Jan 07, 2009 at 09:47 UTC
    One another way is.,
    while(($_ = scalar <>)) { # remove the new line from that line. chomp; if ( /^\s+/ ) { # if there is no previous line then print the warning and cont +inue reading the next line., if ( not defined $cur_line ) { warn "continuation of no previous line\n"; next; } # since it is a continuation line, concatenate it with the previous +line., $cur_line.=$_; } else { # if the line does not contains the tab, then take that line a +s the current line., if (defined $cur_line) { # print the concatenated line. print $cur_line . "\n"; } # store the current line in the variable $cur_line = $_; } }
    Sathiyamoorthy
Re: Appending lines starting with white space to the previous line
by JavaFan (Canon) on Jan 07, 2009 at 13:34 UTC
    Something like (untested):
    $_ = <>; chomp; print; while (<>) { chomp; print "\n" unless s/^\s+//; print; } print "\n";
Re: Appending lines starting with white space to the previous line
by pat_mc (Pilgrim) on Jan 07, 2009 at 14:43 UTC
    Update: Sorry, I just realised, this is in essence what has been suggested by 1Nf3 right at the beginning of this thread ... apologies!

    *********************************************************

    Personally, I like the following two-liner because it is concise, simple and flexible (as illustrated, it will also work with an arbitrary number of indented lines):
    ( my $line = ( join "", <> ) ) =~ s/\n\s+//g; print $line; ______DATA_______ firstline54324563213612531436436463246324634 secondlidfndsfdsfdsfd thirdlinedsfsdf firstline54324563213612531436436463246324634 secondlidfndsfdsfdsfd thirdlinedsfsdf fourthlinedsfsdf fifthlinedsfsdf _____OUTPUT______ firstline54324563213612531436436463246324634secondlidfndsfdsfdsfdthird +linedsfsdf firstline54324563213612531436436463246324634secondlidfndsfdsfdsfdthird +linedsfsdffourthlinedsfsdffifthlinedsfsdf
    Essentially, it compounds the entire data file into a single string from which any occurrence of \n\s+ is then removed. The latter string sequence is the one which occurs at every line end except for those line ends before a firstline.

    Hope this helps.

    Best regards -

    Pat
Re: Appending lines starting with white space to the previous line
by sanku (Beadle) on Jan 08, 2009 at 12:40 UTC
    hi, have a try of this code
    open(FILE,"f1") or die $!; while(<FILE>){if($_!~/^\s+/){push(@array1,'>>'.$_);}else{push(@array1, +$_);}} $array=join('',@array1); $array=~s/\s+//g; $array=~s/>>/\n/g; print $array;
A reply falls below the community's threshold of quality. You may see it by logging in.