Home > Software engineering >  How to read each cell of a column in csv and take each as input for jq in bash
How to read each cell of a column in csv and take each as input for jq in bash

Time:04-26

I am trying to read each cell of CSV and treat it as an input for the JQ command. Below is my code:

line.csv

| Line |
|:---- |
| 11   |
| 22   |
| 33   |
 

Code to read CSV:

while read line

do

   echo "Line is : $line"

done < line.csv

Output:

Line is 11
Line is 22

jq Command

jq 'select(.scan.line == '"$1"') | .scan.line,"|", .scan.service,"|", .scan.comment_1,"|", .scan.comment_2,"|", .scan.comment_3' linescan.json | xargs

I have a linescan.json which have values for line, service, comment_1, comment_2, comment_3

I want to read each value of csv and treat the input in jq query where $1 is mentioned.

Please advice

CodePudding user response:

This is a shot in the dark, as you didn't specify what your output should look like:

jq -sr --slurpfile lineArr line.csv '
    ($lineArr | .[] | tonumber) as $lineNum |
    .[] | select(.scan.line == $lineNum) |
    [
        .scan.line,
        .scan.service,
        .scan.comment_1,
        .scan.comment_2,
        .scan.comment_3
    ] |
    join("|")
' linescan.json
  • Related