Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text editing from tail -f

Status
Not open for further replies.

jgercken

MIS
Feb 13, 2002
304
US
New to this and having problems:

I'm trying to monitor a file with tail -f and pipe it's output to a script that filters based on a file, makes some substitutions, and then dumps to another file.

How would you go about doing this?
-Jeff





----------------------------------------
Wasabi Pop Tarts! Write Kellogs today!
 
Jeff:

You're asking a hard question. The best I've been able to come up with is using the tee command to send the output to another file:

tail -f data.file|tee -a new.file

Since tail -f is actually an endless loop until you interrupt, I don't know what else you can do.

Maybe somebody else smarter than I can help you further.

Regards,

Ed
 
Well tail -f data.file >> new.file seems to work alright as I don't need to see the output. I'm wondering if there is a way to edit strings in a stream. IE cat infile|edit|edit >> output

The goal is to make it psudo realtime and use it to monitor syslog messages.

Also I can't get this to work.
if [ string `fgrep -iv file -f file2`]

It always evaluates as true. Thoughts on this?
-Jeff ----------------------------------------
Wasabi Pop Tarts! Write Kellogs today!
 
It is entirely possible. For example:
Code:
tail -f foo | sed -e 's/foo/bar/' >> bar
However, output is buffered, so you may not see the second file grow immediately. For example. Try the following:
Code:
while true ; do echo foo >> foo ; /bin/ls -l bar; done
Use CTRL-C to get out of this loop. A way of getting a more immediate response is:
Code:
tail -f foo | perl -n -e 'if(/foo/){s/foo/bar/;$|=1;print}' >> bar
I'm sure there are better ways to specify the output file buffering. Anyone?? :)

 
here's how you can do it in a shell script:
Code:
#!/bin/sh
tail -f /var/log/filename | 
   while read field1 field2 field3 field4 etc
      do
         if [ "$field1" = "something" ] ; then
            echo "do this "
         fi
      done
--
Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top