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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read FIle line by line and perform Script 2

Status
Not open for further replies.

aoroca

IS-IT--Management
Oct 3, 2003
39
CO
How I can read a file line by line and compare results with some parameters then performe automatically a script.

I wish read the last line in File1 which is populated periodically.

FILE1

line1
line2
...
...
...
LineN Now read this line
...
...
LineN+X Then i wish read this line and the script
running

The script must read the file and know in what line stay at last time, and so on
 
assuming that "list" is the file you are "reading"

while read listline;

do

echo "$listline"

done < list

where the &quot;echo ....&quot; will be whatever commands you require



Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
If you're trying to &quot;follow&quot; a file that's still being written to, look into using &quot;[tt]tail -f[/tt]&quot;. Something like...
Code:
#!/bin/ksh

tail -f File1.lis | while read LINE
do
    # Do something with $LINE

    print &quot;$(date): ${LINE}&quot;
done
That way, as things are written to the file, they will be picked up and processed by your script.

Hope this helps.

 
Thanks a lot, MiScript work properly, its funny !!
I appreciate your timely information.
Bye and Good Luck
 
Hey SamBones, I need your help again.

I need read line by line a File, and when match some info with grep then does something, why: i.e.



#!/bin/ksh

tail -f File1.lis | while read LINE
error=`grep &quot;something on LINE&quot; LINE`

if [ -n $error] then
#print LINE which match error condition
print &quot;$(date): ${LINE}&quot;
fi
done



But when I run script I have some problems due error match &quot;something on LINE&quot; with all fields in line, How I can resolve this trouble.

 
I don't think I understand your problem. If you mean you only want to process lines that have &quot;[tt]something on LINE[/tt]&quot; in it, then this should work...
Code:
#!/bin/ksh

MATCH=&quot;something on LINE&quot;

tail -f File1.lis | grep &quot;${MATCH}&quot; | while read LINE
do
    # Do something with $LINE

    print &quot;$(date): ${LINE}&quot;
done
This filters for the match pattern before it gets to the while loop.

Plus you forgot the &quot;do&quot; in your example.

Hope this helps.

 
I modified myscript but when it perform read LINE command, dont work, it stay in standby state, look

--------------------------------------
#!/bin/ksh
AA=`date +'%Y'`
MM=`date +'%m'`
DD=`date +'%d'`
ruta=/oracle/rdbms/log
fecha=$AA$MM$DD

MATCH=&quot;Current&quot;

tail -f $ruta/alert_atel.log | grep &quot;${MATCH}&quot; | while read LINE
do
echo &quot;5715851388 $AA$MM$DD ${LINE}&quot; > error_actv.txt;
./$ruta/send_sms error_actv.txt;
print &quot;$(date): ${LINE}&quot;
done
-------------------------------

the output is:

$ sh -x prueba.sh
+ date +%Y
AA=2003
+ date +%m
MM=12
+ date +%d
DD=11
ruta=/oracle/rdbms/log
fecha=20031211
MATCH=Current
+ tail -f /oracle/rdbms/log/alert_atel.log
+ grep Current
+ read LINE


and alert_atel.log is working and populating with info with MATCH=current and dont execute the next part of my script in while loop.

Thanks alot for your time
 
This is the file to check:

tail alert_atel.log
ORA-00600: internal error code, arguments: [2845], [0], [60], [0], [], [], [], []
Thu Dec 11 19:31:26 2003
Errors in file /oracle/rdbms/log/atel_ora_521.trc:
ORA-00600: internal error code, arguments: [2845], [0], [60], [0], [], [], [], []
Thu Dec 11 19:32:12 2003
Thread 1 advanced to log sequence 54310
Current log# 3 seq# 54310 mem# 0: /oracle/dbs/log3atel.dbf
Thu Dec 11 19:33:57 2003
Thread 1 advanced to log sequence 54311
Current log# 1 seq# 54311 mem# 0: /oracle/dbs/log1atel.dbf
$ date
Thu Dec 11 19:34:07 GMT 2003
$ tail alert_atel.log
Current log# 2 seq# 54312 mem# 0: /oracle/dbs/log2atel.dbf
Thu Dec 11 19:37:40 2003
Thread 1 advanced to log sequence 54313
Current log# 3 seq# 54313 mem# 0: /oracle/dbs/log3atel.dbf
Thu Dec 11 19:38:00 2003
Errors in file /oracle/rdbms/log/atel_ora_521.trc:
ORA-00600: internal error code, arguments: [2845], [0], [60], [0], [], [], [], []
Thu Dec 11 19:39:20 2003
Thread 1 advanced to log sequence 54314
Current log# 1 seq# 54314 mem# 0: /oracle/dbs/log1atel.dbf

and my script dont work properly, if you see MATCH=current is ok in some lines... what happend?
 
And something like this ?
Code:
tail -f $ruta/alert_atel.log | while read LINE
do
  case &quot;$LINE&quot; in *$MATCH*)
    echo &quot;5715851388 $AA$MM$DD  ${LINE}&quot;>error_actv.txt
    ./$ruta/send_sms error_actv.txt
    print &quot;$(date): ${LINE}&quot;
  esac
done

Hope This Help
PH.
 
PHV tahnks a lot, Your info was timely.
 
I have FILE1
#MOBILE $MESSAGE

I need check if this file exist and if it exists I need perform something with each line starting with #MOBILE, because later FILE1 will be removed and created with other different datas. And script must be checking if FILE1 exist.

How I can setup last script for this tasks?

Thanks a lot
 
if ur are in 'sh' try
Code:
fl=FIL1
if [ -r &quot;$fl&quot; ]
then
    grep \#MOBILE &quot;$fl&quot;

    if [ $? -eq 0 ]
    then
        echo MOBILE found
    fi
fi
and in ksh
Code:
[[ -r &quot;$fl&quot; &&  &quot;`grep -q \#MOBILE $fl`&quot; -eq 0 ]] && echo MOBILE found
pls note that sh can also have comapt script as in ksh



[ponder]
----------------
ur feedback is a very welcome desire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top