script help

Discussion in 'Perl Misc' started by rvaedex23@gmail.com, Oct 19, 2012.

  1. Guest

    I wanted to read a file and select the first 2 characters and if they are 9A or 7A
    I want to zero out columns 18 to 22 and 25 to 29.

    If this can be done in Perl and Bash.

    Can someone assist please. thanks
     
    , Oct 19, 2012
    #1
    1. Advertising

  2. Jim Gibson Guest

    In article <>,
    <> wrote:

    > I wanted to read a file and select the first 2 characters and if they are 9A
    > or 7A
    > I want to zero out columns 18 to 22 and 25 to 29.
    >
    > If this can be done in Perl and Bash.
    >
    > Can someone assist please. thanks


    Is your file text or binary? What do you mean by "zero out"?

    Assuming that 1) your are talking about ASCII text and 2) the
    replacement characters are ASCII '0', here is a short program that
    demonstrates testing each line, replacing characters in the line
    depending upon what the first two characters are, and printing the
    result. If you mean something else, please let us know:

    #!/usr/bin/perl
    use strict;
    use warnings;
    while( my $line = <DATA> ) {
    if( $line =~ /^[79]A/ ) {
    substr($line,18,5) = '00000';
    substr($line,25,5) = '00000';
    }
    print $line;
    }
    __DATA__
    1234566890123456689012345668901234566890
    7A34566890123456689012345668901234566890
    8234566890123456689012345668901234566890
    9A34566890123456689012345668901234566890
    x9A34566890123456689012345668901234566890
    1234566890123456689012345668901234566890
    abcdefghijklmnopqrstuvwxyz0123456789


    This program uses the magic <DATA> file read operator to read data
    lines from the end of the program. In a real program, you would open an
    external file using the open function:

    open( my $in, '<', $myfile ) or die("Can't open $myfile: $!");

    and then use $in in the read operation:

    while( my $line = <$in> ) {

    You would also want to open a new file:

    open( my $out, '>', $newfile ) or
    die("Can't open $newfile for writing: $!");

    and print to the new file handle instead of system output:

    print $out $line;

    See if you can put all of that together and write your own program.
    Post your program here if you have problems.

    Good luck!

    --
    Jim Gibson
     
    Jim Gibson, Oct 20, 2012
    #2
    1. Advertising

  3. SSS Develop Guest

    This is possible - can you please state your exact problem. Try to provide possible input file and expected output file

    ---sss


    On Saturday, 20 October 2012 03:07:03 UTC+5:30, wrote:
    > I wanted to read a file and select the first 2 characters and if they are 9A or 7A
    >
    > I want to zero out columns 18 to 22 and 25 to 29.
    >
    >
    >
    > If this can be done in Perl and Bash.
    >
    >
    >
    > Can someone assist please. thanks
     
    SSS Develop, Oct 22, 2012
    #3
  4. ccc31807 Guest

    On Friday, October 19, 2012 5:37:03 PM UTC-4, wrote:
    > I wanted to read a file and select the first 2 characters and if they are 9A or 7A
    > I want to zero out columns 18 to 22 and 25 to 29.
    > If this can be done in Perl and Bash.
    > Can someone assist please. thanks


    Assuming that you have some kind of delimited file, you can do it like this (in pseudocode)

    open infile
    open outfile
    while (<infile>)
    {
    if(first two characters don't match /[79]A/)
    then write the line to the outfile
    else
    convert the line to an array
    set the array elements [18-22][25-29] to 0
    join the array back to a line
    write the line to the outfile
    }
    close infile
    close outfile
     
    ccc31807, Nov 9, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,830
  2. Rajat
    Replies:
    3
    Views:
    722
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    584
    Jan Kaliszewski
    Jan 7, 2010
  4. Greg
    Replies:
    1
    Views:
    174
    Gunnar Hjalmarsson
    Jun 6, 2005
  5. Replies:
    9
    Views:
    184
Loading...

Share This Page