![使用 SED 连接 CSV 中的两个字段并将它们添加到行尾](https://linux22.com/image/1301000/%E4%BD%BF%E7%94%A8%20SED%20%E8%BF%9E%E6%8E%A5%20CSV%20%E4%B8%AD%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%AD%97%E6%AE%B5%E5%B9%B6%E5%B0%86%E5%AE%83%E4%BB%AC%E6%B7%BB%E5%8A%A0%E5%88%B0%E8%A1%8C%E5%B0%BE.png)
我遇到了一个超出我知识范围的 SED 问题。我有一个太阳能发电厂,它生成以下格式的 CSV 文件
2012-01-08;00:00;171,297;0,000;
2012-01-08;00:05;171,297;0,000;
2012-01-08;00:10;171,297;0,000;
2012-01-08;00:15;171,297;0,000;
2012-01-08;00:20;171,297;0,000;
2012-01-08;00:25;171,297;0,000;
我正在将这些值导入 MySQL 数据库。我希望将前两个值组合起来作为唯一主键。
结果应如下所示:
2012-01-08;00:00;171,297;0,000;2012-01-08-00:00
2012-01-08;00:05;171,297;0,000;2012-01-08-00:05
2012-01-08;00:10;171,297;0,000;2012-01-08-00:10
2012-01-08;00:15;171,297;0,000;2012-01-08-00:15
2012-01-08;00:20;171,297;0,000;2012-01-08-00:20
答案1
我会用awk
这个,而不是sed
:
awk -F';' ' NF {print $0 $1"-"$2}'
在哪里:
-F';'
:定义输入字段分隔符NF
:是字段数,这里用来过滤掉没有字段的行$0
: 是整行$1
和$2
: 是第一个和第二个字段- 我认为,其余部分是不言而喻的。
演示:
% echo '2012-01-08;00:00;171,297;0,000;
2012-01-08;00:05;171,297;0,000;
2012-01-08;00:10;171,297;0,000;
2012-01-08;00:15;171,297;0,000;
2012-01-08;00:20;171,297;0,000;
2012-01-08;00:25;171,297;0,000;' | awk -F';' ' NF {print $0 $1"-"$2}'
2012-01-08;00:00;171,297;0,000;2012-01-08-00:00
2012-01-08;00:05;171,297;0,000;2012-01-08-00:05
2012-01-08;00:10;171,297;0,000;2012-01-08-00:10
2012-01-08;00:15;171,297;0,000;2012-01-08-00:15
2012-01-08;00:20;171,297;0,000;2012-01-08-00:20
2012-01-08;00:25;171,297;0,000;2012-01-08-00:25
答案2
一种使用方式sed
:
sed 's/\(\([^;]*;\)\{2\}\)\(.*\)/\1\2\1/ ; s/;$// ; s/\(.*\);/\1-/' infile
解释:
s/\(\([^;]*;\)\{2\}\)\(.*\)/\1\2\1/ # Match content until second ';' and copy it at the end of the
# line.
s/;$// # Delete last ';'
s/\(.*\);/\1-/ # Substitute last ';' with '-'
结果:
2012-01-08;00:00;00:00;2012-01-08-00:00
2012-01-08;00:05;00:05;2012-01-08-00:05
2012-01-08;00:10;00:10;2012-01-08-00:10
2012-01-08;00:15;00:15;2012-01-08-00:15
2012-01-08;00:20;00:20;2012-01-08-00:20
2012-01-08;00:25;00:25;2012-01-08-00:25
答案3
这可能对你有用:
sed 'h;s/;.*//;H;g;s/\s*\n//' file