LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2005, 01:15 PM   #1
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
How do I isolate a piece of string?


Hello All,

I would like to know how could I isolate a piece of the following string in bash shell:

"Mar 13 04:02:24 bigserver23 : - /var/spool/postfix/fd/private/detector/anvil"

So I could place it in a variable like this:

JustTheDate="Mar 13 04:02:24"

Any thoughts?
 
Old 03-16-2005, 01:19 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would use cut
Code:
JustTheDate=$(echo "Mar 13 04:02:24 bigserver23 : - /var/spool/postfix/fd/private/detector/anvil" | cut -d' ' -f1,2,3)
 
Old 03-16-2005, 01:22 PM   #3
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Original Poster
Rep: Reputation: 30
Actually what I meant to say is that this entry is part of a log file, like /var/adm/messages for example, and I would like to go in there (from another script) and just extract that date line from the first entry in the log file...

...you know how to do that?
 
Old 03-16-2005, 01:40 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
when you say first entry, is it the first line ?

if so :
Code:
mydate=$(head -1 /var/adm/messages)
mydate=$(echo $mydate | cut -d' ' -f1,2,3)
Now if you want to grep the line that contains "/var/spool/postfix/fd/private/detector/anvil"
just replace head -1 with grep -m 1 "/var/spool/postfix/fd/private/detector/anvil"

or to extract all date from log file :
Code:
$mydate=$(cat  /var/adm/messages | cut -d' ' -f1,2,3)

Last edited by keefaz; 03-16-2005 at 01:42 PM.
 
Old 03-16-2005, 01:43 PM   #5
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Original Poster
Rep: Reputation: 30
Duuuuude!!! Thanks a bunch, that did the trick!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 isolate redundent libraries? dejavu_01 Slackware 3 08-24-2005 01:28 AM
911 Dispatch PC Security :: Isolate Internet Viruses danfig General 2 01-05-2005 08:30 PM
Looking for a piece of software jkruer01 Linux - Software 5 07-23-2004 03:52 PM
Elderly PC may have a HW fault - how to isolate/fix? CestusGW Linux - Hardware 3 03-24-2004 04:11 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 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