-7

I have simple simple CSV file as follow (3 columns and 4 rows for that example):

A       , B      , C
A       , ”B,C”  , D
“A,B”   , C      , D
A       , B      , ”C,D”

How to create script that I can enter the column and the row number and it print me back the content?

Note! that the comma between the quotation marks as a parameter and not as a separator..

edit: Wanted output: for arguments: 1,1 (col,row) the given answer will be: A for arguments: 2,2 (col,row) the given answer will be: B,C for arguments: 1,3 (col,row) the given answer will be: A,B for arguments: 3,3 (col,row) the given answer will be: D

Thanks in advanced

1

1 Answer 1

0

a.pl:

#!/usr/bin/perl

use strict;
use warnings;

use Text::CSV_XS qw( );

my $row_num = shift;
my $col_num = shift;

my $csv = Text::CSV_XS->new({ binary => 1, auto_diag => 1 });

while (my $row = $csv->getline(\*ARGV)) {
    next if --$row_num;

    print($row->[$col_num-1], "\n");
    last;
}

a.csv: (What you posted isn't a valid CSV, though Text::CSV_XS probably could handle it with the right options.)

A,B,C
A,"B,C",D
"A,B",C,D
A,B,"C,D"

Output:

$ ./a.pl 1 1 a.csv
A

$ ./a.pl 2 2 a.csv
B,C

$ ./a.pl 3 1 a.csv
A,B

$ ./a.pl 3 3 a.csv
D
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help! If i try to simulate it to bash script like: #!/bin/bash COL=$1 ROW=$2 while IFS=',' read -r col1 col2 col3; do echo -e $col1 done < file.csv IFS=$' \t\n' but as you can see this is not what i need to do... i don't know how to tell the bash script to get only the relevant col a row.. Can you assist?
I try to do what you did but in bash script
@user5789818: Hint. You can't. There is no Text::CSV_XS module for bash
I know :) I'm trying to do what this perl script does but in bash script.. I managed only get a headache until now.
What did you expect? Rewriting 1000 lines of C code into bash code is not usually a piece of cake
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.