Linux 在 BASH 脚本中使用“awk”将列添加到 CSV 文件的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9506810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Column to end of CSV file using 'awk' in BASH script
提问by SirOracle
How do you add a column to the end of a CSV file with using a string in a variable?
如何使用变量中的字符串将列添加到 CSV 文件的末尾?
input.csv
输入文件
2012-02-29,01:00:00,Manhattan,New York,234
2012-02-29,01:00:00,Manhattan,New York,843
2012-02-29,01:00:00,Manhattan,New York,472
2012-02-29,01:00:00,Manhattan,New York,516
output.csv
输出.csv
2012-02-29,01:00:00,Manhattan,New York,234,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhattan,New York,843,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhattan,New York,472,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhattan,New York,516,2012-02-29 16:13:00
awk.sh
文件
#!/bin/bash
awk -F"," '{="2012-02-29 16:13:00" OFS ; print}' input.csv > output.csv
My attempt above in awk.shadded the string to the end but stripped all the comma separators.
我上面在awk.sh 中的尝试将字符串添加到末尾,但删除了所有逗号分隔符。
awk.sh result
awk.sh 结果
2012-02-29 01:00:00 Manhattan New York 234 2012-02-29 16:13:00
2012-02-29 01:00:00 Manhattan New York 843 2012-02-29 16:13:00
2012-02-29 01:00:00 Manhattan New York 472 2012-02-29 16:13:00
2012-02-29 01:00:00 Manhattan New York 516 2012-02-29 16:13:00
Appreciate any help!
感谢任何帮助!
Updated awk.sh
更新了 awk.sh
#!/bin/bash
GAWK="/bin/gawk"
TIMESTAMP=$(date +"%F %T")
ORIG_FILE="input.csv"
NEW_FILE="output.csv"
#Append 'Create' DateTimeStamp to CSV for MySQL logging
$GAWK -v d="$TIMESTAMP" -F"," 'BEGIN {OFS = ","} {=d; print}' $ORIG_FILE > $NEW_FILE
rm -f $ORIG_FILE
采纳答案by Birei
You may add a comma to OFS
(Output Field Separator):
您可以在OFS
(输出字段分隔符)中添加一个逗号:
awk -F"," 'BEGIN { OFS = "," } {="2012-02-29 16:13:00"; print}' input.csv > output.csv
Output:
输出:
2012-02-29,01:00:00,Manhatten,New York,234,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhatten,New York,843,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhatten,New York,472,2012-02-29 16:13:00
2012-02-29,01:00:00,Manhatten,New York,516,2012-02-29 16:13:00
EDITto answer the comment of SirOracle
:
编辑以回答以下评论SirOracle
:
From awk
man page:
从awk
手册页:
-v var=val
--assign var=val
Assign the value val to the variable var, before execution of the program begins. Such
variable values are available to the BEGIN block of an AWK program.
So assign your date to a shell variable and use it inside awk
:
因此,将您的日期分配给 shell 变量并在其中使用它awk
:
mydate=$(date)
awk -v d="$mydate" -F"," 'BEGIN { OFS = "," } {=d; print}' input.csv > output.csv
回答by Jonathan Leffler
I'd do:
我会做:
awk '{ printf("%s,2012-02-29 16:13:00\n", sed 's/$/,2012-02-29 16:13:00/' input.csv > output.csv
); }' input.csv > output.csv
This hard codes the value, but so does your code.
这对值进行了硬编码,但您的代码也是如此。
Or you can use sed
:
或者你可以使用sed
:
awk -F"," 'BEGIN { OFS = "," } ; {="2012-02-29 16:13:00" OFS ; print}' input.csv >output.csv
回答by J?rg Beyer
You can set the OFS (output field seperator):
您可以设置 OFS(输出字段分隔符):
2012-02-29,01:00:00,Manhatten,New York,234,2012-02-29 16:13:00,
2012-02-29,01:00:00,Manhatten,New York,843,2012-02-29 16:13:00,
2012-02-29,01:00:00,Manhatten,New York,472,2012-02-29 16:13:00,
2012-02-29,01:00:00,Manhatten,New York,516,2012-02-29 16:13:00,
which gives me:
这给了我:
FROM_TIME=2020-02-06T00:00:00
TO_TIME=2020-02-07T00:00:00
{ echo -e "$FROM_TIME,$TO_TIME";}>input1.csv
{ echo -e "from_time,to_time"; cat input1.csv;} > input.csv
回答by Rudra Mishra
If anyone wants to create csv file through shell with column names: where first input stored in variables from_time, to_time.
如果有人想通过带有列名的 shell 创建 csv 文件:其中第一个输入存储在变量 from_time、to_time 中。
example:insert two timestamps with from_time and to_time as column names with respective values -
示例:插入两个带有 from_time 和 to_time 的时间戳作为具有各自值的列名 -
CODE-
代码-
##代码##first line storing the values second line responsible for adding column name
第一行存储值 第二行负责添加列名