Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

$/ usage

by mimiandi (Novice)
on Jan 02, 2013 at 21:12 UTC ( #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"

Comment on $/ usage
Select or Download Code
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.

Re: $/ usage
by ww (Bishop) 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 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-19 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls