我遇到了一个超出我知识范围的 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