Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

perl one-liner

by mojobo (Initiate)
on Jun 22, 2005 at 16:45 UTC ( [id://469085]=perlquestion: print w/replies, xml ) Need Help??

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

hello Perl Monks, i'm trying to create a perl one-liner that can take a file, truncate the contents to 0 (i.e. nothing inside) and then append lines to the file..
so far i've been unsuccessful
this is what i have so far:
perl -i -ple 'truncate $1, 0; print q{hello}' ./testpl

thanks, Matt

Replies are listed 'Best First'.
Re: perl one-liner
by Kanji (Parson) on Jun 22, 2005 at 17:02 UTC
    perl -0777 -i -nle 'print q{hello}' test.pl

    ...but as others have pointed out, it's much easier to handle this sort of thing with shell redirection.

        --k.


      Hi Kanji, i didn't see your reply when i was posting mine..
      thanks! that's perfect..
      thanks to everyone for their help.
      hi guys,
      actually shell redirection is what i have been doing in the past (without perl), but i wanted to have a one-liner that can be called externally. The basic idea is to automate editing a file with different access permissions than the user, via sudo, and not have to chown the file first to the user..
      i can already edit a file if there is content in the file, without using shell redirection, using a perl one-liner like this:
      perl -i -ne 'print unless 1 .. 100; print q{testing} if$.==1' somefile

      of course this doesn't truncate the file, which is what i was interested in attempting..
Re: perl one-liner
by tlm (Prior) on Jun 22, 2005 at 16:55 UTC

    $1 doesn't do what I think you think it does. In Perl, what you're calling $1 is called $ARGV[ 0 ]. Perl's $1 is used to refer to the first of a regular expression's "captured" matches. See perlretut and perlre.

    Anyway, if I'm guessing correctly what you have in mind, and I am the happy beneficiary of a typical Unix shell, and I want to use perl to do this, I'd do:

    perl -le 'print q{hello)' > test.pl

    the lowliest monk

Re: perl one-liner
by friedo (Prior) on Jun 22, 2005 at 16:53 UTC
    So in other words, you just want to overwrite a file? Why not just open it with '>'?
Re: perl one-liner
by Transient (Hermit) on Jun 22, 2005 at 16:53 UTC
    Why do you need perl for that?

    echo "hello" > file
    (and it works on Win32 and UNIX!)

    Unless I'm misreading the question...

    Update: If this is just an exercise to use truncate...
    perl -i -e 'truncate <>,0; print "HELLO";' mytest

    Update: trying to figure out the right way to do this...

    Update^3:
    perl -i -pe 'truncate $ARGV[0], 0; print "Hello"; last' test.pl

    do note that if the file doesn't exist or is empty already, this won't work.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-19 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found