Search code examples
bashshellawksh

Shell Script - get field/column of csv that contains value pass by array


Shell Script - get field/column of csv that contains value pass by array

sample data of inputdata1.txt file has value like -

id name gender

sample data of test.csv having multiple field/column some of has value id with value like 2 row of test.csv

/"id":"21"/, /"p1":"abc"/,  /"id":"22"/  
/"id":"23"/, /"p2":"zyx"/,  /"id":"24"/,  /"id":"25"/

similar has multiple row

Require output - get those column only has contains like id & I can get whole row but I want that column only

output like of below format 2 row of csv

/"id":"21"/ /"id":"22"/
/"id":"23"/ /"id":"24"/ /"id":"25"/

Facing issue "${keywords[$j]}" does not return any value if I used for print only It's work with this command having issue

awk -F, '{for (i=1;i<=NF;i++) if ($i~ "${keywords[$j]}") {print $i}}' test.csv

my code:

#!/bin/bash

declare -a keywords=(`cat inputdata1.txt`)

length=${#keywords[@]}

for (( j=0; j<length; j++ ));
do
awk -F, '{for (i=1;i<=NF;i++) if ($i~ "${keywords[$j]}") {print $i}}' test.csv
#awk -F, '{ for (i=1;i<=NF;i++) if($i~ "id") {print $i}}' test.csv

printf "Current index %d with value %s\n" $j "${keywords[$j]}"

done

Require output - get those column only has contains like id & I can get whole row but I want that column only

  1. If I try to remove ' from command the it give me error - :
syntax error near unexpected token `('
test.sh: line 14: `  awk -F, {for (i=1;i<=NF;i++) if ($i~ "${keywords[$j]}") {print $i}} request.csv'
  1. If I try to add " then error is
awk: syntax error at source line 1
 context is
    {for (i=1;i<=NF;i++) if >>>  (~ <<< 
awk: illegal statement at source line 1

Solution

  • Using GNU awk for arrays of arrays and sorted_in:

    $ cat tst.sh
    #!/usr/bin/env bash
    
    awk '
    { gsub(/^[[:space:]]+|[[:space:]]+$/,"") }
    NR==FNR {
        for ( i=1; i<=NF; i++ ) {
            tag = $i
            gsub(/^\/"|".*$/,"",tag)
            vals[tag][NR] = (NR in vals[tag] ? vals[tag][NR] " " : "") $i
        }
        next
    }
    {
        PROCINFO["sorted_in"] = "@ind_num_asc"
        for ( i=1; i<=NF; i++ ) {
            tag = $i
            if ( tag in vals ) {
                for ( rowNr in vals[tag] ) {
                    print vals[tag][rowNr]
                }
            }
        }
    }
    ' FS=', *' test.csv FS=' ' inputdata1.txt
    

    $ ./tst.sh
    /"id":"21"/ /"id":"22"/
    /"id":"23"/ /"id":"24"/ /"id":"25"/