LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-25-2007, 10:15 AM   #1
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Rep: Reputation: 15
tail -f utilization


hi everybody,
i'm a principiant on linux system so excuse me for my following stupid question...

i have a LOGFILE which i need to view as soon as the events come...moreover i need to apply a filter on the events

now i use:
tail -f LOGFILE | grep PATTERN

the problem is that my LOGFILE has the following feature:
DATE
INFO1
INFO2
INFO3
INFO4
INFO5

but i need "tail" command presents me data in this way:

DATE - INFO1 - INFO3 - INFO5

what's the command line i have to use?

THANKS A LOT!!!
 
Old 06-25-2007, 04:12 PM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Try this :
Quote:
tail -f LOGFILE | grep PATTERN | while read line
do
printf "$line - "
done
 
Old 06-25-2007, 08:14 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Beware that you'll have a delayed output as the pipe processing is buffered.
 
Old 06-26-2007, 03:29 AM   #4
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Original Poster
Rep: Reputation: 15
I tried Agrouf suggestion:

tail -f LOGFILE | grep PATTERN | while read line
do
printf "$line - "
done

but i received this warning:
line 3: printf: --: invalid option
printf: usage: printf format [arguments]

moreover....do you know how can i print only some lines? for example first, second and fifth?

THANKS A LOT!!
 
Old 06-26-2007, 04:31 AM   #5
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Original Poster
Rep: Reputation: 15
i tried this way:

tail -f LOGFILE|grep PATTERN|while {read line} {
printf \n
set lineCount 0
if {lineCount==0||lineCount==3||lineCount==4} then {printf "$line "}
incr lineCount
}

but i receive this error:

line 6: syntax error near unexpected token `}'
line 6: `}'


PLEASE, WHERE IS THE ERROR?!?!?

thanks
 
Old 06-26-2007, 05:03 AM   #6
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
The error is in the use of curly braces {} to enclose the list of commands.

Change this:
tail -f LOGFILE|grep PATTERN|while {read line} {
printf \n
set lineCount 0
if {lineCount==0||lineCount==3||lineCount==4} then {printf "$line "}
incr lineCount
}

to this:
tail -f LOGFILE|grep PATTERN|while {read line}
do
printf \n
set lineCount 0
if {lineCount==0||lineCount==3||lineCount==4} then {printf "$line "}
incr lineCount
done
 
Old 06-26-2007, 05:05 AM   #7
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Original Poster
Rep: Reputation: 15
Question

no way, this is the error:


line 7: syntax error near unexpected token `done'
line 7: `done'
 
Old 06-26-2007, 05:19 AM   #8
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Are you using bash?
Anyway, try this :
Quote:
tail -f LOGFILE | grep PATTERN | awk 'NR==1||NR==4||NR==5 {printf $0 " - "}'
awk syntax is the same, no matter the shell.
 
Old 06-26-2007, 05:20 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I don't understand about the logfile feature you are talking about.
Could you post a portion of the logfile. Piping through grep or sed should filter out unwanted lines.

Code:
DATE
INFO1
INFO2
INFO3
INFO4
INFO5
If there is a blank line between INFO5 and the next date, you can use "awk" and change the record separater to a blank line. However, you won't see output until the end of the record.
 
Old 06-26-2007, 05:33 AM   #10
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
Quote:
Originally Posted by mfran2002
no way, this is the error:


line 7: syntax error near unexpected token `done'
line 7: `done'
BTW you forgot fi after if.

That should be :

tail -f LOGFILE|grep PATTERN|while read line
do
echo
# set lineCount 0 ==> is it tcsh?
lineCount=0 # bash style
if [ $lineCount=0 -o $lineCount=3 -o $lineCount=4 ]
then
printf "%s " $line # may work with your printf?
fi
lineCount=$(expr $lineCount + 1)
done

I believe you are confusing C and bash... ?

Last edited by Agrouf; 06-26-2007 at 05:46 AM.
 
Old 06-26-2007, 06:09 AM   #11
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Original Poster
Rep: Reputation: 15
Talking

sorry,
but i don't know what's my shell

how can i check it?

thanks

PS: i told you i'm a beginner...
 
Old 06-26-2007, 06:22 AM   #12
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
What is the output of :
grep $USER /etc/passwd | cut -d ":" -f 7
 
Old 06-26-2007, 06:38 AM   #13
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Original Poster
Rep: Reputation: 15
this is the output:

/bin/bash
/sbin/nologin


i tried last "Agrouf" suggestion but i received this kind of otuput:

+ printf '%s ' Date: 6/26/07 1:23:49 PM
Date: 6/26/07 1:23:49 PM ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' Logname: NAME12
Logname: pippo ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' Logtype: TYPE12
Logtype: TYPE ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' LogUser: USER12
LogUser: USER ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' LogClient: CLIENT12
LogClient: CLIENT ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' LogServer: SERVER12
LogServer: SERVER ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' LogLife: LIFE12
LogLife: LIFE ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' LogContent: CONTENT12
LogContent: CONTENT ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'
+ printf '%s ' --
-- ++ expr 0 + 1
+ lineCount=1
+ read line
+ lineCount=0
+ '[' 0=0 -o 0=3 -o 0=4 ']'


i inserted an "echo off" command at the begin of the script but anything is changed...


For "jschiwal": this is what my tail|grep returns to me; i need only lines 1, 3, 4

...
Date: 6/26/07 1:33:14 PM
LogName: NAME1
Logtype: TYPE1
LogUser: USER1
LogClient: CLIENT1
LogServer: SERVER1
LogLife: LIFE1
LogContent: CONTENT1
--
Date: 6/26/07 1:33:15 PM
LogName: NAME2
Logtype: TYPE2
LogUser: USER2
LogClient: CLIENT2
LogServer: SERVER2
LogLife: LIFE2
LogContent: CONTENT2
--
Date: 6/26/07 1:33:16 PM
LogName: NAME3
Logtype: TYPE3
LogUser: USER3
LogClient: CLIENT3
LogServer: SERVER3
LogLife: LIFE3
LogContent: CONTENT3
...
 
Old 06-26-2007, 07:15 AM   #14
mfran2002
Member
 
Registered: Nov 2004
Posts: 32

Original Poster
Rep: Reputation: 15
to complete infos:

i need a logfile like this:

...
Date: 6/26/07 1:33:14 PM - Logtype: TYPE1 - LogUser: USER1
Date: 6/26/07 1:33:15 PM - Logtype: TYPE2 - LogUser: USER2
Date: 6/26/07 1:33:16 PM - Logtype: TYPE3 - LogUser: USER3
...
 
Old 06-26-2007, 07:18 AM   #15
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
what is the output of
Code:
grep ^$USER: /etc/passwd | cut -d ":" -f 7
echo off is a dos command.
try
Code:
set +x
and
Code:
tail | grep | awk 'NR%9==1||NR%9==3 {printf $0 " - "} NR%9==4 {printf $0 "\n"}'

Last edited by Agrouf; 06-26-2007 at 07:35 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU utilization in 2.4 vs 2.6.. lynz Linux - Software 2 06-04-2007 01:40 PM
NIC utilization Mike_the_Man Linux - Networking 3 05-20-2007 05:40 PM
Help on gpg utilization Thulemanden Linux - Security 2 10-19-2005 05:30 AM
Best utilization of the channel ivanatora Linux - Networking 0 07-19-2004 01:25 PM
Memory Utilization Question toddclark Linux - General 1 11-04-2000 06:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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