Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

$/ usage

by mimiandi (Novice)
on Jan 02, 2013 at 21:12 UTC ( [id://1011347]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks -- quick question for ya guys i thought i could slurp the whole file from command line using the following code, havent had any luck. wonder if can't do this from the commandline -- if i replaced the /n with // it shows up in one line but was wondering why it wouldnt work with undef $/ Thanks and Happy New Year!

perl -e 'undef $/; $text=<>; print $text; $/="\n";' list

list contains the following information
server1 ipaddress
server2 ipaddress
server3 ipaddress
etc ..
graff: thanks -
Went with the following to do what i wanted :

perl -e 'undef $/; $text=<>; $text =~ tr/\n//; 1 while $text =~ s/\b( +\w+\d+\s*\d+\.\d+\.\d+\.\d+)\s*\1\b/$1/ig; print $text; $/="\n"; list

basically deletes the duplicates entries one after the other with "slurping"

Replies are listed 'Best First'.
Re: $/ usage
by graff (Chancellor) on Jan 02, 2013 at 21:41 UTC
    If you were expecting the $/="\n"; at the end of your one-liner to do something, that's your problem. That step doesn't do anything.

    The undef $/; at the beginning does make sure that all the data in your list file gets slurped into $text, so when you do print $text; you should see the full content of the list, in its original form (as stored in the file).

    Did you want to see something different from that?

      "in its original form" - i was expecting to see something like this: server1 ip server2 ip server3 ip etc..
      I was going to do another pattern match on the string $text after getting it in the string.

      perl -e 'undef $/; my $text=<>; 1 while $text =~ s/\b(\w+\d+)\s*\1\b/$ +1/gi; pring $text; $/="\n";' list
        To get all the lines from a slurped input file to show up as a single line on output, you want something like this:
        perl -e 'undef $/; $_=<>; tr/\n/ /; s/ $/\n/; print' list
        Or you could just use unix/linux commands that do the same thing:
        xargs echo -n < list
        (If you want a clean line break at the end of that output, add  && echo at the end.)
Re: $/ usage
by ww (Archbishop) on Jan 02, 2013 at 21:38 UTC

    I can think of only two reasons why you "can't do this from the commandline" - one is that the file name you're supplying does NOT exist or does not exist where you think it does -- is, in the directory from which you're executing your code.

    The other is that you're on a windows box and need to fix your quotes.

    List.txt for use with 1011347 server1 ipaddress server2 ipaddress server3 ipaddress D:\> perl -E "undef $/; $text=<>; print $text; $/=\"\n\";" List.txt

    Works for me (the oneliner is as revised for use on the windows box most readily available):

    D:\>perl -E "undef $/; $text=<>; print $text; $/=\"\n\";" List.txt

    Source file as output by code above

    List.txt for use with 1011347 server1 ipaddress server2 ipaddress server3 ipaddress D:\> perl -E "undef $/; $text=<>; print $text; $/=\"\n\";" List.txt D:\>

    Or, I suppose, a third possible cause of failure would amount to both the above.   :)

Re: $/ usage
by 7stud (Deacon) on Jan 03, 2013 at 02:08 UTC

    There's no need to slurp the file:

    1) perl -nle '$result .= " $_"; END{print $result}' data.txt 2) perl -e "chomp(@lines=<>); print join ' ', @lines" data.txt

    As for this:

    Went with the following to do what i wanted :

    perl -e 'undef $/; $text=<>; $text =~ tr/\n//; 1 while $text =~ s/\b( +\w+\d+\s*\d+\.\d+\.\d+\.\d+)\s*\1\b/$1/ig; print $text; $/="\n"; list

    basically deletes the duplicates entries one after the other with "slurping"

    1) As graff already pointed out:

    If you were expecting the $/="\n"; at the end of your one-liner to do something, that's your problem. That step doesn't do anything.

    The values of perl's global variables are set to the defaults when a perl program starts up. So setting a global variable in the last line of a perl program does nothing. Once a program ends, all the values that were assigned to any global variables during the program are lost.

    2) Your regex doesn't work:

    use strict; use warnings; use 5.010; my $text = 'S55 1.1.1.1 S66 2.2.2.2 S55 1.1.1.1'; $text =~ s/\b(\w+\d+\s*\d+\.\d+\.\d+\.\d+)\s*\1\b/$1/ig; say $text; --output:-- S55 1.1.1.1 S66 2.2.2.2 S55 1.1.1.1

    3) Why would you ever try to cram so much code into the command line when you can write a perl program in a text file that is easier to write, edit and maintain? In any case, see if this does what you want:

    perl -nle '$results{$_}=undef; END{print join " ", keys %results}' dat +a.txt

    Note that the order of the ip addresses in the output will be random.

Re: $/ usage
by Anonymous Monk on Jan 02, 2013 at 21:27 UTC

      using Deparse shows the following
      $ perl -MO=Deparse,-P -e 'undef $/; $text=<>; print $text; $/="\n";' list
      undef $/;
      $text = <ARGV>;
      print $text;
      $/ = "\n";
      -e syntax OK
      Not familiar with Data::Dump module but i did tried with -0777 but the result was same.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-19 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found