How to read a dynamically changing file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a dynamically changing file
# 1  
Old 04-14-2012
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
Code:
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.
# 2  
Old 04-14-2012
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/
# 3  
Old 04-14-2012
@ 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)

Last edited by kashif.live; 04-14-2012 at 05:59 PM..
# 4  
Old 04-14-2012
Try:
Code:
tail -f file

# 5  
Old 04-14-2012
@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.

Code:
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
# 6  
Old 04-15-2012
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:
Code:
tail -f logfile |
while IFS= read -r line
do
  ...
done > test.out


Last edited by Scrutinizer; 04-15-2012 at 02:21 AM..
# 7  
Old 04-15-2012
@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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamically changing environment variable

Linux Redhat, BASH Shell. I want to put this in my .bash_profile I have log files that go to directory paths based in part on other variables such as went DB Name is set in memory. So if the DB Name changes the path to the log file changes. How do I create an environment variable I put into... (6 Replies)
Discussion started by: guessingo
6 Replies

2. Programming

Changing the way arguments are read from program

I have the following piece of code. Currently the command line arguments are passed as shown below using the "= "sign. I capture the name of the argument, for example vmod and it's corresponding user parameter which is jcdint-z30.cmd. ./raytrac vmod=jcdint-z30.cmd srFile=jcdint.sr Now I want... (12 Replies)
Discussion started by: kristinu
12 Replies

3. UNIX for Dummies Questions & Answers

Read a file dynamically

Hi my requriment is read the file name dynamically my code is #!/bin/sh file="/c/work/loan/" Header_Trailer_move() { sed '1d;$d' $file| cat >sam.txt } Header_Trailer_move in above given path my list of files or there i have to read file dyanamically when i entered particular file name... (2 Replies)
Discussion started by: sgoud
2 Replies

4. Shell Programming and Scripting

Replace characters then read the file without changing it

Hi All At the moment the following code works but ideally i do not want to have to change the original $1 tr "\r" "\n" < "$1" > "$1.fix" printf "\n" >> "$1.fix" mv "$1.fix" "$1" FILE=$1 coffee_out="splitmovie" coffee_fill="-splitAt" coffee_end="-self-contained -o output.mov $2"... (1 Reply)
Discussion started by: babajuma
1 Replies

5. Linux

Changing eth3 to read eth0

Hello everyone I recently clone a system and doing so it gave my nics cards id's of eth3 and eth4 instead of eth0 and eth1. Is there a config file or something out there where I can change these back? All help will be appreciated. (2 Replies)
Discussion started by: aojmoj
2 Replies

6. Shell Programming and Scripting

Problem with changing directory and subdirectories to read only

I have a directory with its subdirectories and files. I want to change them all to read only. Say it is ~/test chmod -R 444 ~/test chmod: `/home/myname/test': permission denied I do not understand. Do I have to have executable mode for a diirectory to access. How can I change ~/test to... (5 Replies)
Discussion started by: lalelle
5 Replies

7. Programming

read() without changing atime ?

Hey, First of all I want to know How do I see the atime of a file ?? Whats the command ?? I think ls -l shows the last modified time right ? Because when I use cat to read a file, the timestamp shown by ls -l does not change. Its not ls -lu ! man ls did not help ! How do I see the last... (8 Replies)
Discussion started by: tantric
8 Replies

8. UNIX for Dummies Questions & Answers

How can i read array elements dynamically in bash?

Hi friends how can we read array elements dynamically in bash shell? (1 Reply)
Discussion started by: haisubbu
1 Replies

9. Shell Programming and Scripting

How to read a dynamically changing file and load into Oracle?

I have a tab delimited file which has 27 character fields. The file needs to be loaded into an Oracle table. But the challenge is that everytime the file comes it may or may not have values in all 27 fields. Column Definition of the 27 fields: TYPE: Char (1) NAME: Char (30) CUSTOM_VAL: Char... (8 Replies)
Discussion started by: madhunk
8 Replies
Login or Register to Ask a Question