Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Making my file all one line

by Anonymous Monk
on Jun 06, 2003 at 01:20 UTC ( [id://263573]=perlquestion: print w/replies, xml ) Need Help??

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

I have a file that I want to make it all into one line: Current file looks like this:
line one line info line two info line three more lines etc
I want it to look like this:
line one line info line two info line three more lines etc
I dont seem to be getting it to work with my attempt here:
use strict; open IN, "theFileName" or die "$!"; while (<IN>) { chomp; my ($line) = (split //n/); print $line; close IN or warn "$!";
Can someone advise how I can get this to work?

Replies are listed 'Best First'.
Re: Making my file all one line
by ehdonhon (Curate) on Jun 06, 2003 at 01:25 UTC
    /usr/bin/perl -pe 'chomp' theFileName

      Unfortunately that joins words on sucessive lines together. Using the input given above:
      $ perl -pe 'chomp' file line one line infoline two infoline threemore lines etc

      --
      John.

        Yup. Good point.

        perl -pe 'chomp; s/(\S)\s*$/$1 /;' file

        The other thing I failed to mention is if you actually want to alter the file and not just dump to the screen, you would want to run 'perl -pi -e'.

        Update: jmcnamara msg'ed me and correctly pointed out that my previous regexp didn't work at all, and if it did it would have also created one space for every blank line. :(

Re: Making my file all one line
by Enlil (Parson) on Jun 06, 2003 at 01:43 UTC
    A couple things to get this to work:
    1. you don't need to split the line on anything. But if you had you would need to change the /n to \n. Since you have not messed with $/, you will only be reading one line time from the file anyhow. If the purpose of this line (the one with split in it), was to put something in $line you would be better of just using: while (my $line = <IN>) { ... } If on the other hand the purpose was to remove the newline at the end of the string, well chomp has already done that for you.
    2. You need to add a closing brace between the print line and the close line.
    If nothing else though Perl should have told you about the missing closing curly bracket, and should have also warned that split //n/ is invalid, the first time you attempted to run your program.

    Anyhow, hope this helps. Though seriously, if you had problems with this you might want to head over to learn.perl.org, browse the online library there, or pick up a copy of the Learning Perl (aka The Llama), and also there are some fine Tutorials around here.

    -enlil

Re: Making my file all one line
by jmcnamara (Monsignor) on Jun 06, 2003 at 08:39 UTC

    Here is a one-liner. It preserves a final "\n" if one or more exists.
    perl -00 -pe 's/ $/\012/ if s/\n+/ /g and eof' file
    Here are some variations:
    perl -00 -pe '(eof) ? s/\n\n/\n/ : s/\n+/ /g ' file perl -00 -pe 's/\n+/(eof) ? "\n" : " "/eg ' file
    Choose eof or eof() to get the desired effect when dealing with multiple files.

    --
    John.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-23 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found