-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

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I've try to think like the example that shown here: http://stackoverflow.com/questions/14492590/using-bash-sed-awk-to-extract-rows-and-columns-in-csv-files – user5789818 Jan 14 '16 at 15:14

1 Answers1

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
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 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? – user5789818 Jan 14 '16 at 15:23
  • I try to do what you did but in bash script – user5789818 Jan 14 '16 at 15:33
  • @user5789818: Hint. You can't. There is no `Text::CSV_XS` module for bash – Borodin Jan 14 '16 at 16:05
  • I know :) I'm trying to do what this perl script does but in bash script.. I managed only get a headache until now. – user5789818 Jan 15 '16 at 23:10
  • What did you expect? Rewriting 1000 lines of C code into bash code is not usually a piece of cake – ikegami Jan 16 '16 at 00:19
  • I'm sure there is a way to do that in bash script.. and I'll try until I will found the way :) I don't give up so fast :) :) – user5789818 Jan 16 '16 at 00:42
  • Of course there is. Any program can be written in bash. Borodin was exaggerating. It doesn't mean it's easy. Again, covering 1000 lines of C code into bash is not easy. It works so so so much longer. How far have you gotten with your parser? – ikegami Jan 16 '16 at 00:42