在shell脚本中插入多个变量

时间:2020-12-30 23:35:57

I'm trying to produce multiple output files from a large JSON file of many objects which can be identified by $var1.

我正在尝试从许多对象的大型JSON文件生成多个输出文件,这些文件可以通过$ var1进行标识。

My 'application' splits arrays within each json object into multiple rows of text which I can test with awk to get records within a period.

我的'应用程序'将每个json对象中的数组拆分成多行文本,我可以用awk测试它以获取一段时间内的记录。

From a long list of $var1, and fixed $unixtime1 and $unixtime2 for a single pass, what would a bash FOR loop look like that would pass in these variables?

从$ var1的长列表,以及单个传递的固定$ unixtime1和$ unixtime2,bash FOR循环看起来会传递这些变量?

cat data.json | grep $var1 | application | awk -F '$10 > $unixtime1 && $10 < $unixtime2' > $var1.txt

I could use jq to obtain a list of var1, but cannot use jq for the entire operation because of some jq limitations with large integers.

我可以使用jq来获取var1的列表,但由于大整数的jq限制,不能将jq用于整个操作。

jq -c .var1 data.json | sort | uniq -c

As an occasional procedure, I've chosen not to modify my 'application' for the task (yet) - but I appreciate the experienced expertise out there.

作为一个偶然的程序,我选择不修改我的“应用程序”来完成任务 - 但我很欣赏那里经验丰富的专业知识。

The following for loop does the first bit - But how do I introduce the timestamps as variables?

以下for循环执行第一位 - 但如何将时间戳作为变量引入?

test.txt
351779050870307
351889052023932

#! /usr/bin/env bash
for i in `cat test.txt`
do
cat data.json | grep $i | application | awk -F "\"*,\"*" '$10 > 1417251600000 && $10 < 1417338000000' > $i.txt
done

2 个解决方案

#1


1  

I THINK this is what you're looking for:

我认为这就是你要找的东西:

while IFS= read -r i
do
    grep "$i" data.json | application |
    awk -F '"*,"*' -v ut1="$unixtime1" -v ut2="$unixtime2" '$10 > ut1 && $10 < ut2' > "$i.txt"
done < test.txt

#2


1  

For those with expertise in things other than BASH (like me) - I hope it helps you with your small script experiment

对于那些对BASH以外的东西(像我一样)有专业知识的人 - 我希望它可以帮助你完成小脚本实验

Here is an example of inserting multiple variables into a bash script that contains a loop and mixes multiple piped functions - The example includes an example of a user prompt, a calculation and a list (test.txt) as input.

下面是一个将多个变量插入到包含循环并混合多个管道函数的bash脚本中的示例 - 该示例包括用户提示,计算和列表(test.txt)作为输入的示例。

The while loop seems tighter than a for loop (from previous answer) (but look up for vs while logic)

while循环似乎比for循环更紧密(来自上一个答案)(但是在逻辑时查找vs)

The interesting syntax of the awk function comes from the previous answer without explanation, but works

awk函数的有趣语法来自前面的答案,没有解释,但有效

#! /usr/bin/env bash
read -e -p "start time?" Unixtime1
Unixtime2=$(echo $Unixtime1 + 86400000 | bc)
while IFS= read -r i
do
grep "$i" data.json | application |
awk -F '"*,"*' -v ut1="$Unixtime1" -v ut2="$Unixtime2" '$10 > ut1 && $10 < ut2' > "$i.dat"
done < test.txt

#1


1  

I THINK this is what you're looking for:

我认为这就是你要找的东西:

while IFS= read -r i
do
    grep "$i" data.json | application |
    awk -F '"*,"*' -v ut1="$unixtime1" -v ut2="$unixtime2" '$10 > ut1 && $10 < ut2' > "$i.txt"
done < test.txt

#2


1  

For those with expertise in things other than BASH (like me) - I hope it helps you with your small script experiment

对于那些对BASH以外的东西(像我一样)有专业知识的人 - 我希望它可以帮助你完成小脚本实验

Here is an example of inserting multiple variables into a bash script that contains a loop and mixes multiple piped functions - The example includes an example of a user prompt, a calculation and a list (test.txt) as input.

下面是一个将多个变量插入到包含循环并混合多个管道函数的bash脚本中的示例 - 该示例包括用户提示,计算和列表(test.txt)作为输入的示例。

The while loop seems tighter than a for loop (from previous answer) (but look up for vs while logic)

while循环似乎比for循环更紧密(来自上一个答案)(但是在逻辑时查找vs)

The interesting syntax of the awk function comes from the previous answer without explanation, but works

awk函数的有趣语法来自前面的答案,没有解释,但有效

#! /usr/bin/env bash
read -e -p "start time?" Unixtime1
Unixtime2=$(echo $Unixtime1 + 86400000 | bc)
while IFS= read -r i
do
grep "$i" data.json | application |
awk -F '"*,"*' -v ut1="$Unixtime1" -v ut2="$Unixtime2" '$10 > ut1 && $10 < ut2' > "$i.dat"
done < test.txt