LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-08-2018, 03:30 AM   #1
cor9957
LQ Newbie
 
Registered: Dec 2006
Location: IJmuiden, the Netherlands
Distribution: CentOS, Fedora, Ubuntu
Posts: 24

Rep: Reputation: 10
How yo cut date and time from line in BASH


Hi everybody,

Not sure if this is the right forum, but here goes.

I use grep to find a particular line in the secure log file, the line looks something like this:
/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX

I need to cut out the date and time into two variables $CLDATE and $CLTIME but I have no idea to do this. Usually I try to find a unique character and use that as delimiter in the cut command, but that won work on this line.

Can anybody point me in the right direction on how to do this so I'll end up with the variable $CLDATE containing "Feb 4" and the variable $CLTIME containing "12:42:09"

Kind regards,
Cor.
 
Old 02-08-2018, 04:26 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
grep will do the job - see the manpage for character classes; a mild form of regex. For example, the following will extract the date above, and allow for 2 digit dates as well.
Code:
grep -oE "[[:alpha:]]{3} [[:digit:]]{1,2}"
Some assumptions taken ...
 
1 members found this post helpful.
Old 02-08-2018, 08:06 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,367

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
Just for the record, it can also be done using bash parameter expansion.
Code:
#!/bin/bash

str="/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX"

str1=${str#*:}
str2=${str1#*[[:space:]]*[[:space:]]}

CLDATE=${str1%%[[:space:]][[:digit:]][[:digit:]]:*}
CLTIME=${str2%%[[:space:]]*}

echo "CLDATE=$CLDATE"
echo "CLTIME=$CLTIME"
 
1 members found this post helpful.
Old 02-08-2018, 09:32 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just for fun
Code:
echo '/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX'| cut -d':' -f2-4|cut -d' ' -f1,2
Feb 4

echo '/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX'| cut -d':' -f2-4|cut -d' ' -f3
12:42:09
 
1 members found this post helpful.
Old 02-09-2018, 12:26 AM   #5
cor9957
LQ Newbie
 
Registered: Dec 2006
Location: IJmuiden, the Netherlands
Distribution: CentOS, Fedora, Ubuntu
Posts: 24

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by syg00 View Post
grep will do the job - see the manpage for character classes; a mild form of regex. For example, the following will extract the date above, and allow for 2 digit dates as well.
Code:
grep -oE "[[:alpha:]]{3} [[:digit:]]{1,2}"
Some assumptions taken ...
Thank you for your help, I've tried it, but this is the output from the command :

grep: /var/log/secure:Feb: No such file or directory
grep: 4: No such file or directory
grep: 12:42:09: No such file or directory
grep: standic-ad: No such file or directory
grep: sshd[27839]:: No such file or directory
grep: pam_unix(sshd:session):: No such file or directory
grep: session: No such file or directory
grep: closed: No such file or directory
grep: for: No such file or directory
grep: user: No such file or directory
grep: sra-Administrator: No such file or directory

I'm probably doing somethig wrong, but what?

Cor.
 
Old 02-09-2018, 12:29 AM   #6
cor9957
LQ Newbie
 
Registered: Dec 2006
Location: IJmuiden, the Netherlands
Distribution: CentOS, Fedora, Ubuntu
Posts: 24

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by allend View Post
Just for the record, it can also be done using bash parameter expansion.
Code:
#!/bin/bash

str="/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX"

str1=${str#*:}
str2=${str1#*[[:space:]]*[[:space:]]}

CLDATE=${str1%%[[:space:]][[:digit:]][[:digit:]]:*}
CLTIME=${str2%%[[:space:]]*}

echo "CLDATE=$CLDATE"
echo "CLTIME=$CLTIME"
Thank you for your help!
I tried this solution, but this is the output I get:

CLDATE=Feb 4
CLTIME=

The CLTIME is empty. Can you tell what I'm doing wrong here?

Cor.
 
Old 02-09-2018, 12:47 AM   #7
cor9957
LQ Newbie
 
Registered: Dec 2006
Location: IJmuiden, the Netherlands
Distribution: CentOS, Fedora, Ubuntu
Posts: 24

Original Poster
Rep: Reputation: 10
I just noticed something strange, when I grep the line from the logfile there are 2 spaces between "Feb"and "4", but in a string there is only 1 space. Is this normal behavior?
Below is what I did:

Code:
Command: grep 27839 /var/log/secure* | grep "session closed"
Output: /var/log/secure:Feb  4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX

Command: str=$(grep 27839 /var/log/secure* | grep "session closed")
Command: echo $str
Output: /var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX
Cor.

Last edited by cor9957; 02-09-2018 at 12:50 AM.
 
Old 02-09-2018, 12:55 AM   #8
cor9957
LQ Newbie
 
Registered: Dec 2006
Location: IJmuiden, the Netherlands
Distribution: CentOS, Fedora, Ubuntu
Posts: 24

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by chrism01 View Post
Just for fun
Code:
echo '/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX'| cut -d':' -f2-4|cut -d' ' -f1,2
Feb 4

echo '/var/log/secure:Feb 4 12:42:09 server-name sshd[27839]: pam_unix(sshd:session): session closed for user UserX'| cut -d':' -f2-4|cut -d' ' -f3
12:42:09
Thanks for your reply, this seems to work "out of the box".
The only problem I see is when the date has two digits.

Cor.
 
Old 02-09-2018, 09:43 AM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,367

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
Quote:
I just noticed something strange, when I grep the line from the logfile there are 2 spaces between "Feb"and "4", but in a string there is only 1 space.
Quote:
Command: str=$(grep 27839 /var/log/secure* | grep "session closed")
Poor use of grep leading to shell removing extraneous space.
Better would be
Code:
str=$(grep "27839.*session closed" /var/log/secure*)
To handle the 2 spaces, change the code in post #3 to
Code:
str2=${str1#*[[:digit:]][[:space:]]}
PS - You have been shown three different approaches. The criterion 'point me in the right direction on how to do this' has been fulfilled. Your data, your requirement and your responsibility to follow through. Please take ownership. We all learn best by working through our own problems.
 
Old 02-09-2018, 11:36 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I don't know what log you're using, pls excuse that fail, but
Code:
sudo grep -oE "[[:alpha:]]{3} [[:digit:]]{1,2}" /var/log/dmesg
gets it.
Code:
BAR 6
mem 0
pci 00
BAR 6
mem 0
pci 00
mem 0
fff 64
hold on...

Last edited by BW-userx; 02-09-2018 at 11:39 AM.
 
Old 02-09-2018, 12:12 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
ok..
for:
Quote:
I need to cut out the date and time into two variables $CLDATE and $CLTIME but I have no idea to do this. Usually I try to find a unique character and use that as delimiter in the cut command, but that won work on this line.
Code:
#!/bin/bash

while read f ; do
echo $f
# $CLDATE and $CLTIME

#looking at my secure log to get patterns to use.
#Date and time is all you're wanting?
hosty=`hostname`
#my host name has slack64.current.org strip it off and use for pattern
hosty=${hosty%%.*}
echo $hosty
strip1=${f%$hosty*}
echo $strip1
#leaving just the month day, time to get
#Feb 8 12:54:08
#timePattern
pattime="[0-9][0-9]:[0-9][0-9]:[0-9][0-9]"
CLDATE=${strip1/$pattime}
echo "date : $CLDATE"
CLTIME="$(echo -e "$strip1" | egrep -o "$pattime")"
echo "time : $CLTIME"
done<<<"$(sudo cat /var/log/secure)"
#done<<<"$(cat ~/scripts/LQ/secureTestFile)"
sample from my secure log
Code:
Feb  8 19:45:05 slack64 sudo:  userx64 : TTY=pts/0 ; PWD=/home/userx64 ; USER=root ; COMMAND=/usr/sbin/dmidecode                                     
Feb  8 20:47:49 slack64 polkitd[1199]: Unregistered Authentication Agent for unix-session:/org/freedesktop/ConsoleKit/Session1 (                     system bus name :1.9, object path /org/gnome/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
results
Code:
slack64
Feb 7 15:56:40
date : Feb  7  
time : 15:56:40
concerning a 2 digit date
Code:
Feb 10 12:05:58 slack64 last message repeated 4 times
slack64
Feb 10 12:05:58
date : Feb 10  
time : 12:05:58
Feb 28 12:06:25 slack64 last message repeated 2 times
slack64
Feb 28 12:06:25
date : Feb 28  
time : 12:06:25
Feb 9 12:07:53 slack64 last message repeated 3 times
slack64
Feb 9 12:07:53
date : Feb 9  
time : 12:07:53
that code works for that as well.

server-name
that is you starting point for your pattern. it looks to me, seeing what little of your log you posted in the first post.
to strip this leading part off
/var/log/secure:
Code:
$ strip="/var/log/secure:feb 32 23:43:34"
$ stripy1=${strip#*:}
$ echo $stripy1
feb 32 23:43:34

Last edited by BW-userx; 02-09-2018 at 01:12 PM.
 
Old 02-10-2018, 01:18 AM   #12
cor9957
LQ Newbie
 
Registered: Dec 2006
Location: IJmuiden, the Netherlands
Distribution: CentOS, Fedora, Ubuntu
Posts: 24

Original Poster
Rep: Reputation: 10
Wow!

You guys are the best!!! Great advise, like Allend says you learn best working through your own problems and he's right, so I'll take it from here.

You have been great help, I can't thank you enough. I still have a lot to learn!

Kind regards,
Cor van den Berghe
 
Old 02-10-2018, 08:56 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by cor9957 View Post
Wow!

You guys are the best!!! Great advise, like Allend says you learn best working through your own problems and he's right, so I'll take it from here.

You have been great help, I can't thank you enough. I still have a lot to learn!

Kind regards,
Cor van den Berghe
Now you can take what you've learned and expand on it.
 
Old 02-11-2018, 06:37 PM   #14
BudiKusasi
Member
 
Registered: Apr 2017
Distribution: Artix
Posts: 345

Rep: Reputation: 15
Code:
CLDATE=`sed -r 's/.*?((jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+[0-9][0-9]?).*/\1/I' secure.log`

CLTIME=`sed -r 's/.*?([0-9][0-9]:[0-9][0-9]:[0-9][0-9]).*/\1/' secure.log`

Last edited by BudiKusasi; 02-11-2018 at 08:29 PM.
 
Old 02-11-2018, 06:55 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by BudiKusasi View Post
Code:
CLDATE=`sed -r 's/.*?((?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d\d?).*/\1/I' secure.log`

CLTIME=`sed -r 's/.*?(\d\d:\d\d:\d\d).*/\1/' secure.log`
did you test this , I fixed the path to log, btw.
Code:
bash-4.3# CLTIME=`sed -r 's/.*?(\d\d:\d\d:\d\d).*/\1/' /var/log/secure`
the read out inside of that VAR is horrendously long...
this one,
Code:
bash-4.3# CLDATE=`sed -r 's/.*?((?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+\d\d?).*/\1/I' /var/log/secure`
sed: -e expression #1, char 73: Invalid preceding regular expression
 
  


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
How to cut a field from a special line in bash script? massy Programming 5 05-04-2014 04:36 AM
[SOLVED] w. bash open file, cut n char from begin of each line, wrt shortened lines to new fil DearWebby Programming 3 12-14-2010 01:28 AM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
Cut out a date/time stamp by using SED and scripting studdard Programming 4 03-01-2009 11:58 AM
Put a date/time from the command line Jzarecta Linux - Software 1 10-03-2007 12:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:43 AM.

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