Better way to do tailing with awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Better way to do tailing with awk
# 1  
Old 10-17-2013
Better way to do tailing with awk

my current code:

varA=$(tail -200 /var/log/data.txt | egrep -c "Capital|capitol")
varB=$(tail -200 /var/log/data.txt | egrep -c "State|Country")
varC=$(tail -200 /var/log/data.txt | egrep -c "City|Town")

I want to do this a different way. something like:

AllVars=$(echo $(tail -200 /var/log/data.txt |  awk '/Capital|capitol/ {++c} END {print +c}' ;  awk '/State|Country/ {++c} END {print +c}' ; awk '/City|Town/ {++c} END {print +c}'))
varA=$(echo ${AllVars} | awk '{print $1}')
varB=$(echo ${AllVars} | awk '{print $2}')
varC=$(echo ${AllVars} | awk '{print $3}')

im pretty sure what i just did here is not efficient. i'm hoping someone can shorten it for me using awk.
# 2  
Old 10-17-2013
You are right, this is not efficient. Pass through the file one time and use an awk script; the following shell script embeds the call to awk...
tail -200 /var/log/data.text | awk '
/[Cc]apital/ { varA=$0}
/State|Country/ { varB=$0 }
/City|Town/ { varC=$0}'

Just to point you in the right direction.
This User Gave Thanks to blackrageous For This Post:
# 3  
Old 10-17-2013
You can accomplish the task efficiently in sh with a while loop to read and a case statement to match and assign.

This User Gave Thanks to alister For This Post:
# 4  
Old 10-17-2013
If you want to put the output of the single awk into shell variables, you could try something like this:
read varA varB varC << EOF
  tail -200 /var/log/data.text | 
  awk '
    /Capital|capitol/ {a++}
    /State|Country/   {b++}
    /City|Town/       {c++} 
    END {print a+0,b+0,c+0}

Last edited by Scrutinizer; 10-17-2013 at 10:01 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-17-2013
An alternate method is
eval $(
  tail -200 /var/log/data.text |
  awk '
    /Capital|capitol/ {a++}
    /State|Country/ {b++}
    /City|Town/ {c++}
    END {print "varA="a+0, "varB="b+0, "varC="c+0}

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 10-18-2013
thank you so much guys. this just saved me a lot of time!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitution to remove tailing slash?

Heyas I am trying to remove a tailing space, with substitution. Already tried some multiple escapes with no luck, is that even possible? echo $CHROOT || \ CHROOT="${CHROOT/\/$}" echo $CHROOT return And all i get is: :) paths $ CHROOT=/usr/local/ + paths $ . * /usr/local/ /usr/local/... (2 Replies)
Discussion started by: sea
2 Replies

2. Shell Programming and Scripting

[BASH] Getting a semi-tailing backslash when passing (escaped) variables to script

Heyas Figured me had a 'typo' in tui-conf-set, i went to fix it. Now, i also figured, it might be nice to have tui-conf-set report (to console, not only exit code) wether it could save the variable to the file or not. This said, I appended this code: (the tui-title and tui-echo lines are... (3 Replies)
Discussion started by: sea
3 Replies

3. Shell Programming and Scripting

awk logic for tailing and printing

if the last line of an output contains a certain string 'FAILED', i want to print 200 lines from the output. here's where I got stuck: process blah blah blah | awk '{if ($0 ~ /FAILED/) y=x "\n" $0; x=$0};END{print y}' the above only prints the last 2 lines, and it also searches the... (19 Replies)
Discussion started by: SkySmart
19 Replies

4. Shell Programming and Scripting

Tailing logs from different files into one single file

Hi Please help me in finding a solution for tailing multiple log files and writing all of them into one common file. I have 4 log files with same name in 4 different folders. Whenever I post a Request - any one of these 4 log files gets updated with some log detail in the below format : ... (5 Replies)
Discussion started by: nisav
5 Replies

5. Shell Programming and Scripting

Tailing and counting lines

tail -f /var/log/syslog | egrep -c FATAL is there a way to do the above and actually have the number of lines matching the pattern increment as it is logged to the log file? for instance, when you invoke a command like the one i just posted, you'll not get the total lines unless you do... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. UNIX for Dummies Questions & Answers

tailing a file which contains Control chracters

Hi. I have a log file which gets updated by a java process and it uses ASCII STX and ETX characters (i.e CTRL-B and CTRL-C characters) to demarcate each XML message logged. so the format of the file is something like STX XML_MESSAGE1 .. .. ETX STX XML_MESSAGE2 .. .. ETX each XML... (4 Replies)
Discussion started by: gregoryp
4 Replies

7. Shell Programming and Scripting

Tailing last modified part of log file

I have a log file which contains data like this This log file is updated twice a day at 7am and 6pm, I want a script(which i will make run at 7:10am and 6:10pm) which should fetch only the last appended lines since last update.. I mean.. if i execute the script at 7.10am 3/3/2010 it... (4 Replies)
Discussion started by: user__user3110
4 Replies

8. Shell Programming and Scripting

Tailing new log file & echo the string on console

Guys, I do have a script that runs to take the server out from network, after running the script it is writing the new log file{outFile} in to directory . Now what i need is my script should tail the last modified file{outFile} & search the string {Server Status} ans should echo the same at the... (0 Replies)
Discussion started by: raghunsi
0 Replies

9. Shell Programming and Scripting

Tailing 2 or more log files simultaneously PERL

Hi, I am having issue where I have to tail 3 log files continuously (forever) and while I am reading the files , parse them and shove the data into DB. I can do this with one file totally fine but how can I read 3 files at the same time? I am not really looking for code (but would be nice) but... (3 Replies)
Discussion started by: Dabheeruz
3 Replies

10. UNIX for Dummies Questions & Answers

tailing logs

Hi I'd like to achieve the ff functionality; tail -f log | grep keyword ...... and then perform a function. That is, I like to tail a log and when a certain keyword appears I then want my script to play an audio file for example. Any ideas?? Cheers M (1 Reply)
Discussion started by: squeakywheel
1 Replies
Login or Register to Ask a Question