Re: [SLUG] perl - setting output field separator and output record separator?

2007-10-16 Thread Jacinta Richardson

Rick Welykochy wrote:


Try this:

dirs -v | perl -wnla -e 'BEGIN {$\= ; $,=:} print @F;'; echo


That works perfectly, but not quite for the reasons you explained.

$\ is Perl's Output Record Separator, and is printed at the end of every print 
statement.  $/ is Perl's Input Record Separator.  When using -a (autosplit) we 
split on spaces by default.  -l enables automatic end of line processing (ie it 
changes $\).  Thus:


dirs -v | perl -wnla -e 'print @F;'

yields:

0/etc
1/tmp
2/home/jarich

(including the final newline).

$, is Perl's Output Field Separator as you expected.  The begin block isn't 
necessary so we can write:


dirs -v | perl -wnla -e '$,=:; print @F;'

to get the desired:

0:/etc
1:/tmp
2:/home/jarich

Since we're using -l anyway, we could use it to set $\ for us:

dirs -v | perl -wna -l040 -e '$,=:; print @F;'
0:/etc 1:/tmp 2:/home/jarich

or we can do it ourselves:

dirs -v | perl -wna -e '$,=:; $\= ; print @F;'

All the best,

Jacinta
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] perl - setting output field separator and output record separator?

2007-09-27 Thread Rick Welykochy

Sonia Hamilton wrote:


I'm doing this using awk and getting correct results:

$ dirs -v | awk 'BEGIN { OFS=:; ORS= ; } ; { print $1,$2 }'
0:/home 1:/etc 2:~ 



I'm trying to do this using Perl (as gnu awk not available on target
system), and getting stuck:

$ dirs -v | perl -wnla -e 'BEGIN {$\=' '; } ; ($A,$B)[EMAIL PROTECTED]; print 
$A:$B;
 '
syntax error at -e line 1, at EOF
BEGIN not safe after errors--compilation aborted at -e line 1.


Try this:

dirs -v | perl -wnla -e 'BEGIN {$\= ; $,=:} print @F;'; echo


ie I'm trying to set the OFS (output field separator $\). Any ideas what
I'm doing wrong? And how do I set the ORS (output record separator) in
Perl (rather than doing print $A:$B)?


ORS is $,

Source:

perldoc perlvar


cheers
rickw



--
_
Rick Welykochy || Praxis Services

Q: What's the difference between a software sales person and a car sales person?
A: With a car sales person there is a slight chance that he actually knows how 
to drive.
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


[SLUG] perl - setting output field separator and output record separator?

2007-09-27 Thread Sonia Hamilton
I'm doing this using awk and getting correct results:

$ dirs -v | awk 'BEGIN { OFS=:; ORS= ; } ; { print $1,$2 }'
0:/home 1:/etc 2:~ 

I'm trying to do this using Perl (as gnu awk not available on target
system), and getting stuck:

$ dirs -v | perl -wnla -e 'BEGIN {$\=' '; } ; ($A,$B)[EMAIL PROTECTED]; print 
$A:$B;
 '
syntax error at -e line 1, at EOF
BEGIN not safe after errors--compilation aborted at -e line 1.

ie I'm trying to set the OFS (output field separator $\). Any ideas what
I'm doing wrong? And how do I set the ORS (output record separator) in
Perl (rather than doing print $A:$B)?

Thanks perl mongers :-)

(Yes, I'm trying to munge my prompt to show the contents of dirs -v from
pushd/popd. I'll put in a  $.  5 and blah  so it doesn't get too
long...)
-- 
Sonia Hamilton

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] perl - setting output field separator and output record separator?

2007-09-27 Thread Matthew Hannigan
On Fri, Sep 28, 2007 at 12:35:44PM +1000, Sonia Hamilton wrote:
 I'm doing this using awk and getting correct results:
 
 $ dirs -v | awk 'BEGIN { OFS=:; ORS= ; } ; { print $1,$2 }'
 0:/home 1:/etc 2:~ 
 
 I'm trying to do this using Perl (as gnu awk not available on target
 system), and getting stuck:

'a2p' (awk to perl) is pretty awesome for situations like this.

Check it out -- it would be installed if perl is.



Matt

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html