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

时间:2012-02-29 21:20:57

标签: linux bash parsing awk

如何使用变量中的字符串将列添加到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

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

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

感谢任何帮助!

更新了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

4 个答案:

答案 0 :(得分:20)

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

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

输出:

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

编辑以回复SirOracle的评论:

来自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.

因此,请将您的日期分配给shell变量,并在awk

中使用它
mydate=$(date)
awk -v d="$mydate" -F"," 'BEGIN { OFS = "," } {$6=d; print}' input.csv > output.csv

答案 1 :(得分:11)

我会这样做:

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

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

或者您可以使用sed

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

答案 2 :(得分:4)

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

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

给了我:

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,

答案 3 :(得分:0)

如果有人想通过shell用列名创建csv文件: 第一个输入存储在变量from_time,to_time中的位置。

示例:插入两个时间戳记,其中from_time和to_time作为具有各自值的列名-

代码-

files = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] #loop to get all files from folder
for x in files:
    image = "x + 1"
    img2 = cv2.imread('similarImages/' + directory + '/' + image + '.png', cv2.IMREAD_COLOR)
    histSim = cv2.calcHist([img2], [1], None, [256], [0, 256])  # create histo of each image
    np.savetxt('Test/similarImage' + x + '.csv', histSim, delimiter=',')  # save save plots to csv

第一行存储值 第二行负责添加列名