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

One liner to S & R by column

by dirtdog (Monk)
on Dec 23, 2013 at 16:36 UTC ( [id://1068214]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I'm trying to create a one liner to search and replace any occurance of the value Y in column 19 with an H excluding the header record.

Below is a sample of the file:

0TESTHLDGS201312230830201312230000001633 125459W540 CUSIPY DIREXION DAILY 20 YEAR PLUS 188554D205 CUSIPY3D SYSTEMS CORPORATION 188579Y101 CUSIPY3M CO. 1000375204 CUSIPYABB LTD 1002824100 CUSIPYABBOTT LABORATORIES 100287Y109 CUSIPYABBVIE INC

I thought there would be many examples of this on the net, but I couldn't find any and i'm struggling with the solution.

Any help is much appreciated.

Replies are listed 'Best First'.
Re: One liner to S & R by column
by davido (Cardinal) on Dec 23, 2013 at 16:47 UTC

    What have you tried?

    perl -pli.bak -e '$. != 1 and substr($_,18, 1) =~ tr/Y/H/;' filename

    Here's how to read it:

    • Run perl.
    • Open filename for reading.
    • Rename the input file filename.bak Open filename for output and select it as the default for STDOUT.
    • Form an implicit while( <> ) { chomp; ... } continue { print "$_$/" } loop.
    • If $. is 1 (first line of the file), skip to the next iteration.
    • On all subsequent iterations $. will no longer be one; transliterate the 18th (zero-indexed) column from 'Y' to 'H'.

    perlrun, perlintro, and perlop should contain 99% of what you need to learn to get a good start on the next one-liner yourself.

    Update: Added play by play explanation.

    Update2: Fixed issue where files aren't zero-indexed with respect to $. (Thanks Jim,Laurent_R)


    Dave

      If $. is 0 (first line of the file), skip to the next iteration. … On all subsequent iterations $. will no longer be zero…

      The number of the first line of the input file is 1, not 0. So you need to use an explicit comparison operation.

      perl -pli.bak -e 'substr($_, 18, 1) =~ tr/Y/H/ if $. > 1' filename

      Here's the same thing using a regular expression.

      perl -pli.bak -e 's/^.{18}\KY/H/ if $. > 1' filename

      Jim

        Updated (and documented the change). :) Thanks.


        Dave

      Thank you Dave. That worked like a charm!

Re: One liner to S & R by column
by Corion (Patriarch) on Dec 23, 2013 at 16:44 UTC

    So, what code have you already written and how did it fail for you?

      This is what I have so far, which will work, but i would need multiple one liners to include all security types

       perl -p -i -e  's/CUSIPY/CUSIPH/g' <filename>

        If you want to play it safe and list all security types, you can use alternation to list them all (see perlre and perlretut:

        perl -p -i.bak -e 's/(CUSIP|CUSIQ|something else)Y/${1}H'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-24 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found