选择和处理文本csv列
我有一个 csv 文本文件,如
2008-01-14T13:38:37.000,10.36,92.7,43.9,C200801141338s,20080114T133837M583Z044
2008-01-16T11:54:44.100,32.35,85.29,12.0,C200801161154d,20080116T115444M589Z012
...
这很容易输出第一(日期时间),并与最后两列awk '{print $1,$5,$6},但我想重新设置日期时间,喜欢2008-01-14T13:38:37.000到20080114_133837.x。如何制作?谢谢。
回答
仅使用您显示的示例,请您尝试以下操作。
awk '
BEGIN{
FS=OFS=","
}
{
gsub(/-|:/,"",$1)
sub(/T/,"_",$1)
sub(/.[0-9]+$/,".x",$1)
print $1,$5,$6
}
' Input_file
说明:为以上添加详细说明。
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this program from here.
FS=OFS="," ##Setting FS, OFS as comma here.
}
{
gsub(/-|:/,"",$1) ##Globally substituting - OR : with NULL in $1.
sub(/T/,"_",$1) ##Substituting T with _ here in $1.
sub(/.[0-9]+$/,".x",$1) ##Substituting .[0-9]+$ at last of 1st field with .x
print $1,$5,$6 ##Printing 1st, 5th and 6th fields here.
}
' Input_file ##Mentioning Input_file name here.