LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   tail -f utilization (https://www.linuxquestions.org/questions/linux-general-1/tail-f-utilization-564362/)

mfran2002 06-25-2007 10:15 AM

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!!!

Agrouf 06-25-2007 04:12 PM

Try this :
Quote:

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

jlliagre 06-25-2007 08:14 PM

Beware that you'll have a delayed output as the pipe processing is buffered.

mfran2002 06-26-2007 03:29 AM

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!!

mfran2002 06-26-2007 04:31 AM

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

bigrigdriver 06-26-2007 05:03 AM

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

mfran2002 06-26-2007 05:05 AM

no way, this is the error:


line 7: syntax error near unexpected token `done'
line 7: `done'

Agrouf 06-26-2007 05:19 AM

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.

jschiwal 06-26-2007 05:20 AM

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.

Agrouf 06-26-2007 05:33 AM

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... ?

mfran2002 06-26-2007 06:09 AM

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

how can i check it?

thanks

PS: i told you i'm a beginner...

Agrouf 06-26-2007 06:22 AM

What is the output of :
grep $USER /etc/passwd | cut -d ":" -f 7

mfran2002 06-26-2007 06:38 AM

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
...

mfran2002 06-26-2007 07:15 AM

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
...

Agrouf 06-26-2007 07:18 AM

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"}'


All times are GMT -5. The time now is 06:31 AM.