Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Does Perl 5 (or 6?) need another built-in variable for the -F switch?

by jbert (Priest)
on Nov 03, 2006 at 08:45 UTC ( [id://582024]=note: print w/replies, xml ) Need Help??


in reply to Does Perl 5 (or 6?) need another built-in variable for the -F switch?

If it is a one-liner then your perl is probably simple enough to go into " rather than ' (just backlash any $vars or double quotes), so:
export F="\t" perl -F$F -line "munge(@F); print join($F, @F);" file
The join($F, @F) looks a bit evil, but hey.

Sadly, this doesn't work:

F="\t" perl -F$F -line "munge(@F); print join($F, @F);" file
Presumably because the prefixed env var doesn't apply to the shell's expansion of the line.

Does anyone know a trick to make this (shorter) approach work?

Replies are listed 'Best First'.
Re^2: Does Perl 5 (or 6?) need another built-in variable for the -F switch?
by Anonymous Monk on Nov 03, 2006 at 09:43 UTC
    You didn't try that, did you? It's not going to work. See, if $F is interpolated by the shell, you end up with:
    join(t, @F)
    This is wrong on several accounts. First, inside the one-liner, you need to place quotes around $F, otherwise, at best you end up with bare words, but more likely, you end up with something that cannot be compiled (Try F=":") for instance. Second, the shell doesn't know "\t" as something special. It just sees an escaped t, and hence, F="\t" is equivalent to "t".

    Also, the command line switches -line won't do what you expect it to do. It means "chomp of newlines, and add newlines to print, and modify the input file, leaving a backup with extension 'ne'".

    You probably want (untested):

    F=' ' # Enter a tab here. perl -F"$F" -i -wlne "munge(@F); print join('$F',@F);" file

    But you can to better, it can also work if you want to use single quotes for the one liner. The shell's quoting mechanism are superior to Perls in some expects. Exploit them:

    perl -F"$F" -i -wlpe 'munge(@F); $_ = join("'$F'",@F);'
      I tried it with a : seperator, but you're right I didn't use the \t. Mea culpa.
        Odd. Using \t *does* work (in the sense that it compiles) because join(t, @F) is valid Perl, and all it does is generate a warning. Using : however does not work, unless your Perl can compile join(:, @F).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found