Hi There, Good Day !!
I have txt file containing data in the below format. There are many lines, here i have mentioned for example.
cat remo.txt
2/3/2017 file1
3/4/2016 file2
6/6/2015 file5
1/1/2018 file3
4/3/2014 file4
-
-
-
I need to grep
the file names for given date rage from user.
for example --
if user enters any date rages (start date) and (end date) as arguments along with script like below
./script.sh 1/1/2014 1/1/2016
then output will be
4/3/2014 file4
3/4/2016 file2
6/6/2015 file5
Please guide me for this portion of script, i will complete my task.
Thanks in Advance.
RudiC
March 14, 2018, 4:55am
2
Why is 3/4/2016 file2
in the results when end date is 1/1/2016
? And, is the date format mm/dd/yyyy
or dd/mm/yyyy
?
date format is dd/mm/yy
.
RudiC
March 14, 2018, 5:02am
4
And the other question?
Plus, on top: Any attempts / ideas / thoughts from your side?
Actually, i tried to do with convert dates in unix time and compare, but it does not work.
so i am seeking help from this forum.
RudiC
March 14, 2018, 5:16am
6
And the residual question?
Apart form this code, i have to do some more code on the data which script will get between start date and end date. Please help me with the above code, the rest i can finish.
Thanks
RudiC
March 14, 2018, 5:33am
8
Why (i.e. by WHAT logic) is 3/4/2016 file2
part of the result output when end date is 1/1/2016
?
I am sorry..I have typed wrongly.
The output should be
4/3/2014 file4
6/6/2015 file5
RudiC
March 14, 2018, 6:05am
10
Why the reversal of lines?
Try
awk -F"[ /]" -vSTART="1/1/2014" -vENDDT="1/1/2016" '
function MLDT(D, M, Y) {return Y * 1E4 + M * 100 + D
}
BEGIN {split (START, TMP)
ST = MLDT(TMP[1], TMP[2], TMP[3])
split (ENDDT, TMP)
EN = MLDT(TMP[1], TMP[2], TMP[3])
}
MLDT($1, $2, $3) >= ST &&
MLDT($1, $2, $3) <= EN
' file
6/6/2015 file5
4/3/2014 file4
Hi RudiC,
Thank you. I will try this code. and keep you posted further.
RudiC
March 14, 2018, 7:20am
12
A bit simplified:
awk -vSTART="1/1/2014" -vENDDT="1/1/2016" '
function MLDT(DT) {split (DT, TMP, "/")
return TMP[3] * 1E4 + TMP[2] * 100 + TMP[1]
}
BEGIN {ST = MLDT(START)
EN = MLDT(ENDDT)
}
MLDT($1) >= ST &&
MLDT($1) <= EN
' file
1 Like