How to derive current date and time and append at the end of each line that contains 'Hello'

后端 未结 8 1608
谎友^
谎友^ 2021-02-07 04:08

I have the following file party.txt that contains something like the following:

Hello Jacky
Hello Peter
Bye Johnson
Hello Willy
Bye Johnny
Hello Mar         


        
8条回答
  •  名媛妹妹
    2021-02-07 04:16

    This solution should work with any awk:

    awk '/Hello/ {cmd="(date +'%H:%M:%S')"; cmd | getline d; print d,$0; close(cmd)}' party.txt
    

    The magic happens in close(cmd) statement. It forces awk to execute cmd each time, so, date would be actual one each time.

    cmd | get line d reads output from cmd and saves it to d.

提交回复
热议问题