Shell 如何在不将输出写入新文件的情况下将文件转换为日期格式(DD-MMM-yyy)
我在文件中有以下数据,其中是日期列 应转换为如下所示的预期输出 和输出应在同一文件中替换 注意:输出不应写入其他文件 文件中的数据Shell 如何在不将输出写入新文件的情况下将文件转换为日期格式(DD-MMM-yyy),shell,Shell,我在文件中有以下数据,其中是日期列 应转换为如下所示的预期输出 和输出应在同一文件中替换 注意:输出不应写入其他文件 文件中的数据 ABC|2021-05-10T18:25:00.000+0100|TYU|ILP XYZ|2021-05-14T18:25:00.000+0100|IO|JH VBNM|2021-05-19T18:25:00.000+0100|KL|KJ 当文件仅作为一列包含日期记录时,我尝试了这段代码,但效果良好。: 预期输出: ABC|10 MAY 2021|TYU|ILP
ABC|2021-05-10T18:25:00.000+0100|TYU|ILP
XYZ|2021-05-14T18:25:00.000+0100|IO|JH
VBNM|2021-05-19T18:25:00.000+0100|KL|KJ
当文件仅作为一列包含日期记录时,我尝试了这段代码,但效果良好。:
预期输出:
ABC|10 MAY 2021|TYU|ILP
XYZ|14 MAY 2021|IO|JH
VBNM|19 MAY 2021|KL|KJ
,然后循环通过传入未格式化日期的每一行,并打印格式化日期
这里有很多混乱的bash循环和变通方法。在AWK前后打印资料几乎是不可能的,但这是一个开始。对于所显示的示例,请尝试以下
AWK
awk '
BEGIN{
FS=OFS="|"
num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec",month,",")
}
{
split($2,arr,"[-T]")
$2=arr[3]" "month[arr[2]+0] " "arr[1]
}
1
' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of awk program from here.
FS=OFS="|" ##Setting FS and OFS as | here.
##Creating month array with months names here.
num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec",month,",")
}
{
split($2,arr,"[-T]") ##Splitting 2nd field into arr with delimiter of - or T here.
$2=arr[3]" "month[arr[2]+0] " "arr[1] ##Saving arr[3] space month value with index of arr[2] space and value of arr[1] here.
}
1 ##printing current line here.
' Input_file ##Mentioning Input_file name here.
我建议在
while
循环中逐行读取字段,然后用|
拆分字段
cat filename.txt\
|而IFS=“|”读作abc;做
回显“$A |$(日期-d“$B”+“%d%B%Y”)|$C”
完成
第三列和后续列都存储在$C
变量中
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of awk program from here.
FS=OFS="|" ##Setting FS and OFS as | here.
##Creating month array with months names here.
num=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec",month,",")
}
{
split($2,arr,"[-T]") ##Splitting 2nd field into arr with delimiter of - or T here.
$2=arr[3]" "month[arr[2]+0] " "arr[1] ##Saving arr[3] space month value with index of arr[2] space and value of arr[1] here.
}
1 ##printing current line here.
' Input_file ##Mentioning Input_file name here.