0

I have log.txt with sample output like these:

...
10-Feb-2022 15:15:14.099 lorem
10-Feb-2022 15:15:15.133 ipsum
10-Feb-2022 15:15:16.233 dolor
...

I expect the output of filtered log.txt will be

...
1644480914 lorem
1644480915 ipsum
1644480916 dolor
...

I have figured out how to convert date string to timestamp

date --date='10-Feb-2022 15:15:14.099' +"%s"
Output:
1644480914

My brain still don't get it how to apply that date command to log.txt.

Also How do I pipeline that date command?

printf "10-Feb-2022 15.15.17.012 water" | date --date=<what must i put here?> + "%s"

I expect the output of pipelined command is same, the different is without disturb other column, that is 1644480917 water

4
  • see stackoverflow.com/questions/1521462/… then for each line read, split it into date, time and rest, convert and write back out.
    – Bib
    Feb 10, 2022 at 20:07
  • So I need programming method instead of simple command or pipelining method, I think iterate lines of a log file is taking time. Feb 10, 2022 at 20:11
  • Yup, it's about 6 lines of bash script. You have to read line by line.
    – Bib
    Feb 10, 2022 at 20:13
  • Then, I need to know how to process certain column (date string) without disturb other column. Because iterate lines doesn't mean iterate column. Feb 10, 2022 at 20:18

1 Answer 1

2

Your date command seems to allow for the -d option to convey a date/time. Try like

cut -d' ' -f-2 file | date +%s -f- | paste -d' ' - file | cut -d' ' -f1,4-
1644502514 lorem
1644502515 ipsum
1644502516 dolor

It cuts the date/time fields from the input, feeds them to the date command via the -f (read DATEFILE) option, pastes it back to the input file, and cuts out the old date/time fields.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .