使用BASH脚本中的“awk”将列添加到CSV文件的末尾

时间:2021-10-09 00:26:15

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

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"," '{$6="2012-02-29 16:13:00" OFS $6; print}' input.csv > output.csv

My attempt above in awk.sh added the string to the end but stripped all the comma separators.

我在awk.sh上面的尝试将字符串添加到结尾但删除了所有逗号分隔符。

awk.sh result

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

#!/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 = ","} {$6=d; print}' $ORIG_FILE > $NEW_FILE
rm -f $ORIG_FILE

3 个解决方案

#1


16  

You may add a comma to OFS (Output Field Separator):

您可以向OFS(输出字段分隔符)添加逗号:

awk -F"," 'BEGIN { OFS = "," } {$6="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

EDIT to 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 = "," } {$6=d; print}' input.csv > output.csv

#2


8  

I'd do:

我会做:

awk '{ printf("%s,2012-02-29 16:13:00\n", $0); }' input.csv > output.csv

This hard codes the value, but so does your code.

这个硬编码值,但代码也是如此。

Or you can use sed:

或者你可以使用sed:

sed 's/$/,2012-02-29 16:13:00/' input.csv > output.csv

#3


4  

You can set the OFS (output field seperator):

您可以设置OFS(输出字段分隔符):

awk -F"," 'BEGIN { OFS = "," } ; {$6="2012-02-29 16:13:00" OFS $6; print}' input.csv >output.csv

which gives me:

这给了我:

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,

#1


16  

You may add a comma to OFS (Output Field Separator):

您可以向OFS(输出字段分隔符)添加逗号:

awk -F"," 'BEGIN { OFS = "," } {$6="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

EDIT to 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 = "," } {$6=d; print}' input.csv > output.csv

#2


8  

I'd do:

我会做:

awk '{ printf("%s,2012-02-29 16:13:00\n", $0); }' input.csv > output.csv

This hard codes the value, but so does your code.

这个硬编码值,但代码也是如此。

Or you can use sed:

或者你可以使用sed:

sed 's/$/,2012-02-29 16:13:00/' input.csv > output.csv

#3


4  

You can set the OFS (output field seperator):

您可以设置OFS(输出字段分隔符):

awk -F"," 'BEGIN { OFS = "," } ; {$6="2012-02-29 16:13:00" OFS $6; print}' input.csv >output.csv

which gives me:

这给了我:

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,