LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-10-2014, 02:20 PM   #1
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Rep: Reputation: Disabled
Problem extracting date


I have this iptables record:

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.
 
Old 09-10-2014, 03:01 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,623

Rep: Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964Reputation: 7964
Quote:
Originally Posted by battles View Post
I have this iptables record:

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.
 
Old 09-10-2014, 03:05 PM   #3
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-10-2014, 03:40 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
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}'
 
Old 09-10-2014, 03:47 PM   #5
battles
Member
 
Registered: Apr 2014
Distribution: Debian GNU/Linux 7.5 (wheezy)
Posts: 258

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by norobro View Post
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.

Thanks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with extracting .7z file moonsage Linux - Software 4 09-09-2013 02:20 AM
Extracting Date and Time From The at Command Woodsman Linux - General 14 09-11-2009 04:48 AM
problem in extracting files r_s Linux - Newbie 7 09-02-2009 11:45 AM
problem in extracting tarball shariefbe Ubuntu 5 09-28-2008 01:15 PM
MYSQL extracting DATE value ohcarol Programming 1 08-16-2007 01:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration