This is a CSV file. In each row there are fields separated with comma. So here is the idea.
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"; } }
In reply to Re: Search/Replace within fields of CSV
by vinoth.ree
in thread Search/Replace within fields of CSV
by johnmck
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |