更改文件中的日期格式

时间:2022-09-18 10:20:31

I'm working on redHat linux.

我在研究redHat linux。

I've a file which looks like :

我有一个文件是这样的:

$vi filename

美元vi文件名

Jan,1,00:00:01,someone checked your file

1月1 00:00:01,有人检查你的文件

Jan,3,09:38:02,applebee

1月3 09:38:02,阿普尔比连锁餐厅

Jan,16,10:20:03, ****************

10:20:03 1月16日,* * * * * * * * * * * * * * * *

Jan,18,03:04:03, ***************

03:04:03 1月18日,* * * * * * * * * * * * * * *

I want the output to look like:

我希望输出是:

2015/01/01,00:00:01,someone checked your file

2015/01/01 00:00:01,有人检查你的文件

2015/01/03,3,09:38:02,applebee

2015/01/03 3 09:38:02,阿普尔比连锁餐厅

2015/01/16,16,10:20:03, ****************

10:20:03 2015/01/16,16日,* * * * * * * * * * * * * * * *

2015/01/18,03:04:03, ***************

2015/01/18 03:04:03,* * * * * * * * * * * * * * *

Please help me to do this. Thanks

请帮我做这件事。谢谢

1 个解决方案

#1


2  

If you have GNU date, try:

如果您有GNU日期,请尝试:

$ awk -F, '{cmd="date -d \""$1" "$2"\" +%Y/%m/%d"; cmd|getline d; print d","$3","$4; close(cmd)}' file
2015/01/01,00:00:01,someone checked your file
2015/01/03,09:38:02,applebee
2015/01/16,10:20:03, ****************
2015/01/18,03:04:03, ***************

This approach cannot be used with the BSD (OSX) version of date because it does not support any comparable -d option.

此方法不能与BSD (OSX)版本的日期使用,因为它不支持任何可比较的-d选项。

How it works

awk implicitly loops over lines of input, breaking each line into fields.

awk隐式地在输入行上循环,将每一行分解为字段。

  • -F,

    - f,

    This tells awk to use a comma as the field separator

    这告诉awk使用逗号作为字段分隔符

  • cmd="date -d \""$1" "$2"\" +%Y/%m/%d"

    cmd="日期-d \""$1" "$2"\" +%Y/%m/%d"

    This creates a string variable, cmd, and contains a date command. I am assuming that you have GNU date.

    这将创建一个字符串变量cmd,并包含一个日期命令。我假设您有GNU日期。

  • cmd|getline d

    cmd | getline d

    This runs the command and captures the output in variable d.

    这将运行命令并捕获变量d中的输出。

  • print d","$3","$4

    打印d”、“3”、“4美元

    This prints the output that you asked for.

    这将打印您要的输出。

  • close(cmd)

    关上(cmd)

    This closes the command.

    这个关闭命令。

#1


2  

If you have GNU date, try:

如果您有GNU日期,请尝试:

$ awk -F, '{cmd="date -d \""$1" "$2"\" +%Y/%m/%d"; cmd|getline d; print d","$3","$4; close(cmd)}' file
2015/01/01,00:00:01,someone checked your file
2015/01/03,09:38:02,applebee
2015/01/16,10:20:03, ****************
2015/01/18,03:04:03, ***************

This approach cannot be used with the BSD (OSX) version of date because it does not support any comparable -d option.

此方法不能与BSD (OSX)版本的日期使用,因为它不支持任何可比较的-d选项。

How it works

awk implicitly loops over lines of input, breaking each line into fields.

awk隐式地在输入行上循环,将每一行分解为字段。

  • -F,

    - f,

    This tells awk to use a comma as the field separator

    这告诉awk使用逗号作为字段分隔符

  • cmd="date -d \""$1" "$2"\" +%Y/%m/%d"

    cmd="日期-d \""$1" "$2"\" +%Y/%m/%d"

    This creates a string variable, cmd, and contains a date command. I am assuming that you have GNU date.

    这将创建一个字符串变量cmd,并包含一个日期命令。我假设您有GNU日期。

  • cmd|getline d

    cmd | getline d

    This runs the command and captures the output in variable d.

    这将运行命令并捕获变量d中的输出。

  • print d","$3","$4

    打印d”、“3”、“4美元

    This prints the output that you asked for.

    这将打印您要的输出。

  • close(cmd)

    关上(cmd)

    This closes the command.

    这个关闭命令。