develooper Front page | perl.beginners | Postings from July 2012

Re: How to set ">" as record separator in a Perl one liner?

Thread Previous
From:
John W. Krahn
Date:
July 16, 2012 00:48
Subject:
Re: How to set ">" as record separator in a Perl one liner?
Message ID:
5003C74D.4060503@shaw.ca
Michael Brader wrote:
> On 07/16/2012 04:05 PM, De-Jian Zhao wrote:
>>
>> I want to change the record separator in a Perl one liner with ">" as
>> the separator. However, I tried without success.
>>
>> The perlrun document
>> (http://perldoc.perl.org/perlrun.html#Command-Switches) says that*
>> "***-0*[/octal/hexadecimal/] * specifies the input record separator
>> (|$/| ) as an octal or hexadecimal number. *"
>> *When I tried to get the octal/hexadecimal code of ">" with oct(">")
>> and hex(">"), I got "0". I used this number and it did not work the
>> way I wanted (perl -00 -ne 'print if /AAAA/' test.seq ).
>>
>
>  From 'perldoc -f oct'
>
> ...
> oct Interprets EXPR as an octal string and returns the corresponding
> value.
> ...
> To go the other way (produce a
> number in octal), use sprintf() or printf():
>
> $perms = (stat("filename"))[2] & 07777;
> $oct_perms = sprintf "%lo", $perms;
>
> So it is used for converting a string into an octal value. But we can go
> the other way with printf and ord:
>
> perl -e 'printf "%lo\n", ord(q{>})'
> 76
>
> Now perl will leave the input record separator on the string, but we can
> take that off with chop:
>
> echo 'AAAA>BBBB>CCCC' | perl -0076 -nE 'chop,say if /AAAA/'
> AAAA

Better to use chomp (the -l switch) instead of chop:

echo 'AAAA>BBBB>CCCC' | perl -0076nlE 'say if /AAAA/'


> TIMTOWTDI of course, and you could also do it like this:
>
> echo 'AAAA>BBBB>CCCC' | perl -nE 'for (split />/) { say if /AAAA/ }'
> AAAA

echo 'AAAA>BBBB>CCCC' | perl -F> -naE '/AAAA/ && say for @F'

:-)



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About