Formatting date strings in a file with linux bash shell

后端 未结 3 1281
甜味超标
甜味超标 2021-01-23 21:33

When I cat the file an example of the output format is:

ok: servername Mon May 23 00:00:00 EDT 2018
ok: servername Thu Jul 16 00:00:00 EDT 2019

3条回答
  •  情书的邮戳
    2021-01-23 22:06

    With GNU date, you can specify an input file containing all your date strings; combined with cut and paste:

    paste -d ' ' \
        <(cut -d ' ' -f-2 infile) \
        <(date -f <(cut -d ' ' -f3- infile) '+%m/%d/%Y')
    

    Output:

    ok: servername 05/23/2018
    ok: servername 07/16/2019
    

    This uses process substitution to build a temporary input file for date -f, and for paste to build a new output file.

提交回复
热议问题