[英]Edit .csv file via shell script output
I have a script which takes a .csv
file as a an input and runs row by row 我有一个脚本,将
.csv
文件作为输入并逐行运行
kma210,projects.kma210
kma215,projects.kma215_2
KMA3xx,projects.kma3xx
KMI7,projects.kmi7
The above one is a sample for the .csv
file. 上面的是
.csv
文件的示例。 I have two types of process running for each row (example : abc_process and xyz_process). 我为每行运行两种类型的进程(示例:abc_process和xyz_process)。
Now my requirement is once the script runs one row , the output ie, success/fail should print in the same file in third column ( abc_process
) and fourth column ( xyz_process
). 现在,我的要求是,一旦脚本运行一行,输出(即成功/失败)应打印在第三列(
abc_process
)和第四列( xyz_process
)的同一文件中。
Example please see here: 例子请看这里:
kma210,projects.kma210,success,success
kma215,projects.kma215_2,fail,success
KMA3xx,projects.kma3xx,success,fail
KMI7,projects.kmi7,fail,fail
Could anyone please suggest? 有人可以建议吗?
I think what you are trying to achieve is something of this sort. 我认为您想要实现的目标就是这种。
From your input file ( input.csv
), use the script as follows:- 在您的输入文件(
input.csv
)中,使用以下脚本:
#!/bin/bash
while IFS=',' read -r col1 col2
do
# The below two read commands are to process the success/fail status
# string as you indicated. If you are receiving a string of the below
# form, from an external bash command; you need to use process-substitution
# as
# IFS= read -r _ p1Status <<(p1Command)
# IFS= read -r _ p1Status <<(p2Command)
IFS= read -r _ p1Status <<<"abc success"
IFS= read -r _ p2Status <<<"xyz fail"
# The below printf just prints the output as you need to stdout
# to create a .csv out of it, append it to a new file as
# printf "%s,%s,%s,%s\n" "$col1" "$col2" "$p1Status" "$p2Status" >> output.csv
printf "%s,%s,%s,%s\n" "$col1" "$col2" "$p1Status" "$p2Status"
done<input.csv
The above command as such writing to stdout
, produces an output as 上面的命令这样写入
stdout
,生成的输出为
kma210,projects.kma210,success,fail
kma215,projects.kma215_2,success,fail
KMA3xx,projects.kma3xx,success,fail
KMI7,projects.kmi7,success,fail
which you can append to a file using the >>
operator with the line I commented out. 您可以使用
>>
运算符加上我注释掉的行将其追加到文件中。 Or if you want a single file and replace the original file. 或者,如果您想要一个文件并替换原始文件。 Add a line
添加一行
mv -v output.csv input.csv
as the last line of the script. 作为脚本的最后一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.