Issue
I have the input as 11 column file with ; separated.
ARM5914447;2023-10-17 18:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Mark;.com;p4;Vail;.com
ARM5914448;2023-10-18 19:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Tony;.com;p4;Vail;.com
I need to compare the date in 2nd column with today’s date and print the days difference value in 12th column in same file.
i came up with this awk command
awk -F";" 'NR == 1 { print $0";difference of days"; next } { "date -d \"today\" +%s" | getline today; "date -d \""$2"\" +%s" | getline filedate; diff = int((today - filedate) / 86400); print $0";"diff }'
when just printing diff it's looks good but printing $0";"diff it's not appending the 12th column, what i'm missing here
Solution
When I run your script I get the following output:
ARM5914447;2023-10-17 18:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Mark;.com;p4;Vail;.com;difference of days
ARM5914448;2023-10-18 19:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Tony;.com;p4;Vail;.com;14
^^^^^^^^^^^^^^^^^^^
Notice the new data is appended to the end of the lines.
If I convert the file to contain windows/dos line endings (\r\n
) the new data ends up at the beginning of the lines, eg:
$ unix2dos dat_file
$ awk '.....' dat_file
;difference of days17 18:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Mark;.com;p4;Vail;.com
^^^^^^^^^^^^^^^^^^^
;145914448;2023-10-18 19:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Tony;.com;p4;Vail;.com
^^^
Notice the new data appears at the front of the line.
At this point I'm wondering if your script is printing the new data at the beginning of the line and you happened to miss it.
You can check for windows/dos line endings in a couple ways, eg:
$ file dat.file
dat.file: ASCII text, with CRLF line terminators
^^^^^^^^^^^^^^^^^^^^^ - contains windows/dos line endines
$ file dat.file
dat.file: ASCII text
- no mention of 'CRLF' => does not contain windows/dos line endings
$ od -c dat.file
... snip ...
0000140 ; V a i l ; . c o m \r \n A R M 5
^^^^^^ - CRLF ==> windows/dos line endings
... snip ...
0000320 l ; . c o m \r \n
^^^^^^ - CRLF ==> windows/dos line endings
An easy fix would be to remove the \r
characters from the file (eg, run dos2unix
) and then run the awk
script, eg:
$ dos2unix dat.file
$ awk -F";" 'NR == 1 { print $0";difference of days"; next } { "date -d \"today\" +%s" | getline today; "date -d \""$2"\" +%s" | getline filedate; diff = int((today - filedate) / 86400); print $0";"diff }' dat.file
ARM5914447;2023-10-17 18:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Mark;.com;p4;Vail;.com;difference of days
ARM5914448;2023-10-18 19:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Tony;.com;p4;Vail;.com;14
^^^^^^^^^^^^^^^^^^^
Another option to have awk
remove the trailing \r
from each line:
$ awk -F";" '{ sub(/\r$/,"") } NR == 1 { print $0";difference of days"; next } { "date -d \"today\" +%s" | getline today; "date -d \""$2"\" +%s" | getline filedate; diff = int((today - filedate) / 86400); print $0";"diff }' dat.file
^^^^^^^^^^^^^^^^^
ARM5914447;2023-10-17 18:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Mark;.com;p4;Vail;.com;difference of days
ARM5914448;2023-10-18 19:05:38;2023-10-19 16:11:29;Missing name;Reassigned; Release;Tony;.com;p4;Vail;.com;14
^^^^^^^^^^^^^^^^^^^
Answered By - markp-fuso Answer Checked By - Marie Seifert (WPSolving Admin)