Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
0 0 DROP all -- * * 61.4.83.186 0.0.0.0/0 /* CN SSH 7 Sep 10 2014 */
and I need to extract the 'Sep 10 2014' date using grep or sed. The date could be like this also: 'Sep 9 2014'. The date I need to get may not be in the same exact position, so I can't use cut. I need something like:
grep -o '[A-Z]\{1,3\} [0-9]\{1,2\} [0-9]\{1,4\}'
that will search and extract it. I can't get it to work.
0 0 DROP all -- * * 61.4.83.186 0.0.0.0/0 /* CN SSH 7 Sep 10 2014 */
and I need to extract the 'Sep 10 2014' date using grep or sed. The date could be like this also: 'Sep 9 2014'. The date I need to get may not be in the same exact position, so I can't use cut. I need something like:
grep -o '[A-Z]\{1,3\} [0-9]\{1,2\} [0-9]\{1,4\}'
that will search and extract it. I can't get it to work.
Thanks.
Why not use awk, and set the field-delimiter to be the space/tab character?
Code:
awk -F " " '{print $14" "$15" "$16}'
..will do it, and return "Sep 10 2014". You can also use sed, and rip out everything from beginning of line to the SSH, and manipulate it from there:
Code:
sed -n 's/.*SSH //p' <filename>
...which returns "7 Sep 10 2014 */", and is easier to manipulate from there.
Thanks. When I see awk, I tend to need some kind of stiff drink. I have tried to work with awk, but have always failed. I'll use your example, however.
grep works for me using the -P switch, removing the backslashes in the regex and adding a-z for the lower case letters:
Code:
grep -o -P '[A-Za-z]{1,3} [0-9]{1,2} [0-9]{1,4}'
Yes, that is exactly what I needed! My problem is that the 'CN' in my record example above can go missing for some reason, so I needed to go after the date your way. I could have counted the columns and determined what to use for the awk -F " " '{print $14" "$15" "$16}' method, but the grep takes care of that. I didn't have a grep -P example, so I couldn't figure it out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.