Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Perl Newline Help

by vivek.vivek (Initiate)
on Nov 11, 2014 at 17:31 UTC ( #1106853=perlquestion: print w/replies, xml ) Need Help??

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

I have the below file which I pass to a perl code. Usually the format is that 6th Field of the line starting with FILE is a number. But in some cases, the number falls to the next line. The number is important to me. I need to bring the number by replacing "\" to the line starting with FILE. I tried chomp and all. But didnot work.

FILE TEXT VAL 9999.999 08-jul-2014 \

270 3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \

WKENFLWHFELHN UIEKEFJBKJFN

FILE TEXT2 VAL 9999.999 16-may-2014 60 \

8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\

YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF

Below is the latest code I tried.
#! /usr/bin/Perl my $file1 = "file.txt"; open(INOUT1,"<", $file1) or die "cant open output file "; + foreach $line(<INOUT1>) { #chomp $line; if ($line =~ /\\/){ $line =~ s/\\\n//; if ($line =~ /\s+/){ $line =~ s/\s+//; push(@newlines,$line); }} } foreach $linw(@newlines){ if ($linw =~ m/^FILE /) { print $linw,"\n"; next; } }

Output I need is

FILE TEXT VAL 9999.999 08-jul-2014 270

3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \

WKENFLWHFELHN UIEKEFJBKJFN

FILE TEXT2 VAL 9999.999 16-may-2014 60 \

8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\

YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF

Replies are listed 'Best First'.
Re: Perl Newline Help
by Loops (Curate) on Nov 11, 2014 at 18:41 UTC

    Not beautiful, but gets the job done:

    while (<DATA>) { if (/^FILE/ && split(/[\s\\]+/) == 5) { my ($num,$trail) = <DATA> =~ m/^\s*(\d+)\s*(.*)$/s; s/\s*(?=\\\s*$)/ $num /s; print; print $trail; } else { print; } } __DATA__ FILE TEXT VAL 9999.999 08-jul-2014 \ 270 3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \ WKENFLWHFELHN UIEKEFJBKJFN FILE TEXT2 VAL 9999.999 16-may-2014 60 \ 8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\ YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF

    Prints:

    FILE TEXT VAL 9999.999 08-jul-2014 270 \ 3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \ WKENFLWHFELHN UIEKEFJBKJFN FILE TEXT2 VAL 9999.999 16-may-2014 60 \ 8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\ YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF
Re: Perl Newline Help
by 2teez (Vicar) on Nov 11, 2014 at 20:38 UTC

    Or what about this?

    use warnings; use strict; my $file_line; while (<DATA>) { if ( /^FILE/ and !/\s+[0-9]+ \\$/ ) { ( $file_line = $_ ) =~ s/[\s\\]+$/ /; } elsif (/^([0-9]+)\s+?(.*)$/) { $file_line .= $1 . " \\\n" . $2; print $file_line, $/; } else { print; } } __DATA__ FILE TEXT VAL 9999.999 08-jul-2014 \ 270 3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \ WKENFLWHFELHN UIEKEFJBKJFN FILE TEXT2 VAL 9999.999 16-may-2014 60 \ 8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\ YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF
    Output:
    FILE TEXT VAL 9999.999 08-jul-2014 270 \ 3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \ WKENFLWHFELHN UIEKEFJBKJFN FILE TEXT2 VAL 9999.999 16-may-2014 60 \ 8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\ YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF
    Note, it not checking to know the number of fields in the FILE line, but assumed that the line that follows the FILE line should not start with all number and the FILE line should end with all numbers.
    Working just on assumptions and "funny" idea, using if/elsif/else. The OP can take it from here.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Perl Newline Help
by ww (Archbishop) on Nov 14, 2014 at 02:01 UTC

    Just another (ugly) way to tackle this (omitting print of unchanged "FILE..." lines and all data:)

    #!/usr/bin/Perl use strict; use warnings; use 5.018; # 1106853.pl # assumes that the number to be moved up has > 1 digit and that # un-movable lines begin with not_more_than 0 or 1 digits before a non +-digit char. my ($line, @newlines, $linw, $line_may_need_a_fix); my @arr = <DATA>; START: for (my $i=0; $i < @arr; $i++) { $line = $arr[$i]; if ( $line =~ /\d{4} \\$/ ) { $line_may_need_a_fix = $line; next START; } elsif ( $line_may_need_a_fix && ($line =~ /^([0123456789]{2,4})/ +) ) { my $nums = $1; chomp $line_may_need_a_fix; $line_may_need_a_fix =~ s/\\$//; my $fixedLine = $line_may_need_a_fix . $nums; # Left as an ex +ercise: # removing $num +s from $line push(@newlines,$fixedLine); # Likewise, capture of $line an +d un-movables } $line_may_need_a_fix = ''; } foreach $linw(@newlines){ if ($linw =~ m/^FILE /) { print $linw,"\n"; next; } } __DATA__ FILE TEXT VAL 9999.999 08-jul-2014 \ 270 3E4497B6B8482ADED734 HDNDFLHWLEJHFL LKSNFLKN \ WKENFLWHFELHN UIEKEFJBKJFN FILE TEXT2 VAL 9999.999 16-may-2014 60 \ 8EC4B7367989D54F6D6C HSHFGFLALKHF KJAHEFKHAH\ YRNBFLJNELFN LQJFLKJWEF LKJFLKWJF FILE TEXT3 VAL 000.01 06-apr-2014 \ 1313 ABC3DEF XY77ZZ FILE TEXT4 VAL 213.7 05-feb-1013 95 \ 74nbfljaxlon lgggkjaxor blosqr4

    Output:

    FILE TEXT VAL 9999.999 08-jul-2014 270 FILE TEXT3 VAL 000.01 06-apr-2014 1313

    ++$anecdote ne $data


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1106853]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-07-02 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (102 votes). Check out past polls.

    Notices?