This is a CSV file. In each row there are fields separated with comma. So here is the idea.

The algorithm

The process should go like this:

    1.Read in the file line by line.
    2.For each line, extract the 1rd and 5th column and check whatever you want and print. 
Update:
use strict; use warnings; my $file = 'csv_file.csv'; open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\n"; while (my $line = <$fh>) { chomp $line; my @fields = split(/,/, $line); if ($fields[0] eq 'C') { $fields[0] = 'CONDENSED'; } elsif ($fields[0] eq 'F'){ $fields[0]='FINAL' } if ($fields[4] eq '0H') { $fields[4] = '0HIO' } elsif ($fields[4] eq '0R') { $fields[4] = 'OREGON' } print join(',',@fields); print "\n" }

Sorry delayed to update the code with module Text::CSV,

#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new({ sep_char => ',' }); my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; while (my $line = <$data>) { chomp $line; if ($csv->parse($line)) { my @fields = $csv->fields(); if ($fields[0] eq 'C') { $fields[0] = 'CONDENSED'; } elsif ($fields[0] eq 'F'){ $fields[0]='FINAL' } if ($fields[4] eq '0H') { $fields[4] = '0HIO' } elsif ($fields[4] eq '0R') { $fields[4] = 'OREGON' } print join(',',@fields); print "\n" } else { warn "Line could not be parsed: $line\n"; } }

All is well. I learn by answering your questions...

In reply to Re: Search/Replace within fields of CSV by vinoth.ree
in thread Search/Replace within fields of CSV by johnmck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.