How to read a dynamically changing file

I want to read a constantly changing file and do some operation on text found in that file.
Actually that is log file of linux system and whenever i find a matching string in that file i want to create a text file with timestamp. is it possible to read that file?

here is sample output of log file

Apr 14 16:03:38 localhost kernel: usb 1-2.1: new high speed USB device using uhci_hcd and address 9
Apr 14 16:03:38 localhost kernel: usb 1-2.1: New USB device found, idVendor=12d1, idProduct=1037
Apr 14 16:03:38 localhost kernel: usb 1-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Apr 14 16:03:38 localhost kernel: usb 1-2.1: Product: Ideos
Apr 14 16:03:38 localhost kernel: usb 1-2.1: Manufacturer: Huawei Incorporated

Apr 14 16:02:21 localhost kernel: usb 1-1: new full speed USB device using uhci_hcd and address 8
Apr 14 16:02:21 localhost kernel: usb 1-1: New USB device found, idVendor=0951, idProduct=1643
Apr 14 16:02:21 localhost kernel: usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Apr 14 16:02:21 localhost kernel: usb 1-1: Product: DataTraveler G3
Apr 14 16:02:21 localhost kernel: usb 1-1: Manufacturer: Kingston

I want to create a file whenever "New USB device found" text is found in this file. create a file with timestamp in \usr\temp folder.

You can get the information directly from the source by creating a custom udev rule. You can use them to run simple commands whenever a device is detected. See man udev for details and examples, and also check out the udev rules your system already has ( which do things like populate /dev/disk/by-path/ ) which may be under /etc/udev.d/ or /lib/udev/rules.d/

On my system, custom rules are kept in /etc/udev.d/ while system ones are kept in /lib/udev/rules.d/

@ Corona688: thanks for reply. but in my case i can't use udev as it is not allowed to be dependent on it. for me there are 2 ways to check it. one write perl code ( which I can't as I know nothing about perl). 2nd use bash scripting (which i'm trying to do)

Try:

tail -f file

@Scrutinizer: sorry, you didn't get my point.
I know tail -f or tailf can be used to read from some file but i want to put that thing in if condition.
like keep reading this file and when u get string "USB connected". do some action. Now I'm getting problem while using this command in if condition.

while loop (never ending)
              if ( $ (tailf filename) == "USB Connected" ) ----> this is point where I'm getting problem.
              then
              generate alert
              fi

 

hope you understand my query

I got your point, but you had not posted what you tried up to this point. tail -f cannot be used that way since it is never ending. Try this approach:

tail -f logfile |
while IFS= read -r line
do
  ...
done > test.out

@Scrutinizer: can you please explain what is "IFS" in this loop.is it the string that I'm trying to search? and why i need text.out? can't i go without it? and sorry If my question wasn't clear enough.

simply, I just need a way to keep a check all the time on a log file so that whenever some specific text comes into that file, i get alert regarding that text. any function call? or any loop, condition? any way in scripting?

Thanks for your time and efforts.

IFS is is the Internal Field Separator. In the example I set that to "" so that leading white space does not get removed. test.out out is just an arbitrary file name, you can choose whatever file you like or use a pipeline symbol instead to pipe it into something that sends out the alert..