![创建从文件读取数据的文本](https://linux22.com/image/115142/%E5%88%9B%E5%BB%BA%E4%BB%8E%E6%96%87%E4%BB%B6%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%87%E6%9C%AC.png)
我有一个 csv 文件,其中包含以下格式的 600 个数字的列表
442030580306
442030580307
我需要它通过读取上面的数字来将文件输出到一个长文件中,如下所示
"new-request-uri-user-part:442030580306": {
"query_string": {
"query": "new-request-uri-user-part:442030580306",
"analyze_wildcard": true
}
},
"new-request-uri-user-part:442030580307": {
"query_string": {
"query": "new-request-uri-user-part:442030580307",
"analyze_wildcard": true
}
},
有什么想法如何最好地实现这一目标?
答案1
当我们想要最终获得一个JSON数据时 -杰克是正确的工具:
numbers.csv
文件内容:
442030580306
442030580307
杰克解决方案:
jq -s '"new-request-uri-user-part:" as $pfx | reduce [.[]|tostring][] as
$n ({}; .[($pfx + $n)] |= {"query_string": {"query": ($pfx + $n), "analyze_wildcard": true }})' numbers.csv
输出:
{
"new-request-uri-user-part:442030580306": {
"query_string": {
"query": "new-request-uri-user-part:442030580306",
"analyze_wildcard": true
}
},
"new-request-uri-user-part:442030580307": {
"query_string": {
"query": "new-request-uri-user-part:442030580307",
"analyze_wildcard": true
}
}
}
"new-request-uri-user-part:" as $pfx
- 将常用短语分配"new-request-uri-user-part:"
给变量$pfx
reduce [.[]|tostring][] as $n ({}; ...)
- 对输入数字列表执行归约操作
答案2
简单地说,使用 printf:
while read number
do
printf ' "new-request-uri-user-part:%s": {
"query_string": {
"query": "new-request-uri-user-part:%s",
"analyze_wildcard": true
}
},
' "$number" "$number"
done < input