script to tail file; problem with awk and special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to tail file; problem with awk and special characters
# 1  
Old 01-24-2012
script to tail file; problem with awk and special characters

Trying to use code that I found to send only new lines out of a log file by doing:
Code:
while :; do
temp=$(tail -1 logfile.out)
awk "/$last/{p=1}p" logfile.out  #pipe this to log analyzer program
last="$temp"
sleep 10
done

Script works fine when logfile is basic text, but when it contains characters that have brackets and slashes, the awk command has issues:
Code:
awk: fatal: Invalid range end: /[15C8:01E6-0258] 01/

Here is sample data in log:
Code:
[15C8:01EB-1DF8] 01/24/2012 08:03:43 AM Open session 
[15C8:01EB-0310] 01/24/2012 08:03:43 AM  Closed session 
[11A8:000F-06F4] 01/24/2012 08:03:44 AM  Router: Message 0047BCE8 delivered to
[15C8:01EB-1660] 01/24/2012 08:03:44 AM  Opened session 
[15C8:01E6-0258] 01/24/2012 08:03:44 AM  Closed session for


I have tried different ways of putting quotes around the last variable, but does not work or breaks script in different way. Any awk experts have suggestions?

Thx. Moo.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-24-2012 at 10:59 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-24-2012
Another way :
Code:
Last=0
while :
do
    Line=$(wc -l < moo.txt)
    awk "NR>$Last {p=1}p" moo.txt #pipe this to log analyzer program
    Last=$Line
    sleep 10
done

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 3  
Old 01-24-2012
thx jean-Pierre. I added the if statement to your code to handle if log file gets truncated/rolled/etc....
Code:
Last=0 
while : do     
Line=$(wc -l < moo.txt)
    if [ $Line -lt $Last ]     
then          
Last=0     
fi
    awk "NR>$Last {p=1}p" moo.txt #pipe this to log analyzer program     
Last=$Line     
sleep 10 
done


Last edited by Franklin52; 01-25-2012 at 03:52 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handle special characters in awk -F

Hello Folks, Need to bisect strings based on a subset. Below works good. echo /a/b/c/d | awk -F"/c/d$" '{print $1}' /a/b However, it goes awry with special characters. echo /a/b/c+/d | awk -F"/c+/d$" '{print $1}' /a/b/c+/d Desired output: /a/b Escaping the special characters... (11 Replies)
Discussion started by: vibhor_agarwali
11 Replies

2. Shell Programming and Scripting

awk conditions failing (special characters?)

This is really frustrating because I can't figure it out. I'm running a health check script. One of the items I'm checking is the amount of memory on a server. I use the free command, which outputs something like this (excerpt) Mem: 100 100 100 100 Swap: 100 100 100 100 In my debugging... (5 Replies)
Discussion started by: JustaDude
5 Replies

3. Shell Programming and Scripting

Problem with special characters....

grep -i "$line,$opline" COMBO_JUNK|awk -F, ' { C4+=$4 } { } END { print C4 } ' OFS=,` when i run this command in the script.... it o/p all the value as 0 if $line contains any special parameters..... but the same script if i run in command prompt... it shows... (4 Replies)
Discussion started by: nikhil jain
4 Replies

4. Shell Programming and Scripting

Problem with Special characters in file

Hi, I am facing a below problem. Inorder to mak sure the below file is fixed width i am using the following command awk '{printf("%-375s\n", $0) } so as to add trailing spaces at the end for records of length less than 375. Input file > inp.txt 1©1234 1234 123©1 The output file is... (1 Reply)
Discussion started by: marcus_kosaman
1 Replies

5. Shell Programming and Scripting

Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts. I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file. awk -F, '{if ($NF ~ /^|^/) print >"goodfile";else print >"badfile"}' filename sample data 1,abc,def,1234,A * 2,bed,dec,342,* A ... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 Replies

7. Shell Programming and Scripting

special characters giving problem

Hi All, I have a CSV file in which some fields contains special character for ex:- my file is file 1 cat file1 abcd,bgfht,ngbht,abvc **** hdlld,hsgdt,bhfy,knht **** whenever i am trying to put a 4th feild in a variable its giving me list of all the files i have in current... (6 Replies)
Discussion started by: sam25
6 Replies

8. HP-UX

utf-8, problem with special characters

Hi all, We are facing the following problem in our HP-UX machine: software that manipulates utf-8 encoded strings (e.g. during string cut), fails to correctly manipulate strings (all containing Greek characters) that contain special characters like @, &, # etc. Actually, in different... (3 Replies)
Discussion started by: alina
3 Replies

9. Shell Programming and Scripting

Handling special characters using awk

Hi all, How do I extract a value without special characters? I need to extract the value of %Used from below and if its greater than 80, need to send a notification. I am doing this right now..Its giving 17%..Is there a way to extract the value and assign it to a variable in one step? df |grep... (3 Replies)
Discussion started by: sam_78_nyc
3 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question