Hi,
I've no problems filtering text in awk but whenever I try to filter date format such as 03022013, I couldn't get awk to filter the item out.
Any tips?
Hi,
I've no problems filtering text in awk but whenever I try to filter date format such as 03022013, I couldn't get awk to filter the item out.
Any tips?
Can you explain what do you mean by filter?
Please post some sample data and show us what did you try and what issues you are facing?
Hi,
echo -e "Date(ddmmyyyy): \c"
read datte
echo -e "User ID: \c"
read userid
awk /$userid/,/$datte/'{print $1"\t"$2"\t"$6"\t"$5"\t"$4}' samplefile
I am trying to filter out user and login date.
I am running a LAST -command
Make sure the date is in ddmmyyyy
format
Try this code:
awk -v U="$userid" -v L="$datte" '$0~U&&$0~L{ print $1"\t"$2"\t"$6"\t"$5"\t"$4}' samplefile
OR
awk -v U="$userid" -v L="$datte" '$0~U&&$0~L{ print $1,$2,$6,$5,$4}' OFS='\t' samplefile
It still is not clear what you want. If you only want to print lines that match both $userid and $datte, the script bipinajith provided should do what you want.
If you want to print sets of lines starting with a line that matches $userid and want to print that line and the lines following it up to and including the next line that contains $datte, change the &&
in bipinajith's code to ,
.
If you want to print any line that matches $userid or matches $datte, change the &&
to ||
.
Thank you so much for the replies.
I've tried out the code given.
I wanted print any line that matches $userid and matches $datte.
So far when I try enter $userid it can filter but when I enter $datte e.g. 03022013 nothing comes out.
Can you post sample data from your file: samplefile
root :0 Thu Oct 4 11:06 - 11:07 (00:00)
root :0 Thu Oct 4 11:06 - 11:06 (00:00)
root pts/1 :0.0 Thu Oct 4 19:04 - 11:06 (-7:-58)
root :0 Thu Oct 4 19:03 - 11:06 (-7:-57)
root :0 Thu Oct 4 19:03 - 19:03 (00:00)
reboot system boot 2.6.18-128.el5 Thu Oct 4 19:03 (-7:-55)
OK, I see that the date in your file is not in ddmmyyyy
and I clearly said in my previous post to make sure that the date is in ddmmyyyy
format!
So now it is obvious why the code didn't work for date entered.
You have 2 options here, either enter the date as per the format in your file or convert the date to required format using GNU date
:
datte="03022013"
datte=${datte:4:4}${datte:0:2}${datte:2:2}
ndatte=$( date -d"${datte}" +"%a %b" )
day=$( date -d"${datte}" +"%d" )
day=${day#0}
datte="$ndatte $day"
And run the awk
code.