简体   繁体   English

Shell 脚本 - 获取包含数组传递值的 csv 的字段/列

[英]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 Shell 脚本 - 获取包含数组传递值的 csv 的字段/列

sample data of inputdata1.txt file has value like - inputdata1.txt 文件的示例数据的值类似于 -

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 test.csv 的样本数据具有多个字段/列,其中一些具有值 id,其值类似于 test.csv 的 2 行

/"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 - 获取那些仅包含 id 之类的列,我可以获得整行,但我只想要该列

output like of below format 2 row of csv output 如下格式 2 行 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如果我仅用于打印,则面临问题“${keywords[$j]}”不会返回任何值它与此命令一起使用有问题

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需要 output - 获取那些仅包含 id 之类的列,我可以获得整行,但我只想要该列

  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

Using GNU awk for arrays of arrays and sorted_in:将 GNU awk 用于 arrays 的 arrays 和 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"/
awk -v var="${keywords[$j]}" -F, '{for (i=1;i<=NF;i++) if ($i ~ var) {print $i}}' test.csv

got answer thanks you all for your support !!得到答案谢谢大家的支持!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM