Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Converting multiple lines into single line.

by beecha (Initiate)
on Jan 09, 2018 at 12:17 UTC ( #1206966=perlquestion: print w/replies, xml ) Need Help??
beecha has asked for the wisdom of the Perl Monks concerning the following question:

HI All,

happy new year

i am reading text file which contains below multiple lines.

i am reading every line one by one as a string. if the line is started with WAR i need to check for '{' and make all the line into a single line till it ends with '}' without considering the '&' character

EX:

war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32}

CONVERTED TO:

war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4,5*BIN43 +,6*DEC32, 5*BIN43,7*DEC32}
war wear, init =1*DEC1, ev (VARIABLE) { & 1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32,6*DEC32,6*DEC3, & 8*BIN3,6*DEC34 }

CONVERTED TO:

war wear, init =1*DEC1, ev (VARIABLE) { 1*BIN0,2*DEC3,6*BIN4, 5*BIN43, +6*DEC32, 5*BIN43,7*DEC32,6*DEC32,6*DEC3, 8*BIN3,6*DEC34 }

please help me with this how to convert.

2018-01-09 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: Converting multiple lines into single line.
by Laurent_R (Canon) on Jan 09, 2018 at 14:41 UTC
    Hi beecha,

    this was posted 4 days ago on the Perl Guru forum: http://perlguru.com/gforum.cgi?post=84614;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread.

    And both BillKSmith and myself tried to provide working solutions more than two days ago, despite the fact that your initial question there was far less precise and less clear than your OP here above.

    There is nothing wrong with cross posting, but it is considered good practice to inform people that you have posted somewhere else to avoid duplication of work in different parts of the Internet.

    And, whether or not the solutions provided by Bill and myself fully satisfied your needs, I am a bit surprised that you ask again the same thing here but, as of now, did not care to answer our posts helping you on that other forum.

Re: Converting multiple lines into single line.
by Eily (Prior) on Jan 09, 2018 at 13:33 UTC

    To detect that you are in your multiline section you can use the range operator (meaning "from..to") and regular expressions.

    /^war.*[{]/ means a line starting (the ^ means start) by war, followed by any character (the .) many times (the *) and a { ([] contains a list of allowed characters).
    /[}]/ is a string containing }.
    /^war.*[{]/../^war.*[{]/ checks $_ and is true between war.*{ and }. You can try this:

    while (<DATA>) { if (/^war.*[{]/../[}]/) { $_ = "==> $_"; # Show the matching lines } print; } __DATA__ bla bla war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32} bla bla

    Now the tools you may need to complete the work can be: chomp to remove the newline, and tr to remove a character (or maybe s for a more complex modification)

Re: Converting multiple lines into single line.
by tybalt89 (Vicar) on Jan 09, 2018 at 15:44 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1206966 use strict; use warnings; print join('', <DATA>) =~ s/^war \V* \K \{ .*? \}/ $& =~ tr|&\n||dr /e +gmsxr; __DATA__ some starting text war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32} some other text war wear, init =1*DEC1, ev (VARIABLE) { & 1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32,6*DEC32,6*DEC3, & 8*BIN3,6*DEC34 } some final text

    Outputs:

    some starting text war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4, 5*BIN4 +3,6*DEC32, 5*BIN43,7*DEC32} some other text war wear, init =1*DEC1, ev (VARIABLE) { 1*BIN0,2*DEC3,6*BIN4, 5*BIN43, +6*DEC32, 5*BIN43,7*DEC32,6*DEC32,6*DEC3, 8*BIN3,6*DEC34} some final text
Re: Converting multiple lines into single line.
by tybalt89 (Vicar) on Jan 09, 2018 at 22:37 UTC

    On the other hand, if you insist on reading a line-at-a-time, let's just define lines to end on a '}';

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1206966 use strict; use warnings; local $/ = '}'; while(<DATA>) { s/^war \V* \K \{ .* \}/ $& =~ tr|&\n||dr /emsx; print; } __DATA__ some starting text war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32} some other text war wear, init =1*DEC1, ev (VARIABLE) { & 1*BIN0,2*DEC3,6*BIN4, & 5*BIN43,6*DEC32, & 5*BIN43,7*DEC32,6*DEC32,6*DEC3, & 8*BIN3,6*DEC34 } some final text

    Outputs:

    some starting text war eligible, init =0*DEC0, ev (VARIABLE){1*BIN0,2*DEC3,6*BIN4, 5*BIN4 +3,6*DEC32, 5*BIN43,7*DEC32} some other text war wear, init =1*DEC1, ev (VARIABLE) { 1*BIN0,2*DEC3,6*BIN4, 5*BIN43, +6*DEC32, 5*BIN43,7*DEC32,6*DEC32,6*DEC3, 8*BIN3,6*DEC34} some final text

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2018-10-21 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?