'How to read each cell of a column in csv and take each as input for jq in bash

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



Solution 1:[1]

Given the input files and desired output:
  • line.csv
22,Test1
3389,Test2
10,Test3
  • linescan.json
{
  "scan": {
    "line": 3389,
    "service": "Linetest",
    "comment_1": "Line is tested1",
    "comment_2": "Line is tested2",
    "comment_3": "Line is tested3"
  }
}
  • desired output:
Test2 | 3389 | Linetest | Line is tested1 | Line is tested2 | Line is tested3

Here's a solution with jq:

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

jq -sr --rawfile lineArr line.csv '
    (
        $lineArr | split("\n") | del(.[-1]) | .[] | split(",")
    ) as [$lineNum,$prefix] |
    .[] | select(.scan.line == ($lineNum | tonumber)) |
    [
        $prefix,
        .scan.line,
        .scan.service,
        .scan.comment_1,
        .scan.comment_2,
        .scan.comment_3
    ] |
    join(" | ")
' linescan.json

Update: with jq 1.5:
#!/bin/bash

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1