Preserve space in variable of AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve space in variable of AWK
# 8  
Old 02-01-2011
It "worked" because compared strings within 1 month (January).
When the month rolled into February, the string comparison doesn't work any more, because 'Feb' comes before 'Jan' alphabetically.

What're your sample records from the daemon.log?
# 9  
Old 02-01-2011
Code:
Feb  1 22:07:53 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:07:53 server2 snmpd[800]: last message repeated 8 times
Feb  1 22:07:57 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:07:57 server2 snmpd[800]: last message repeated 92 times
Feb  1 22:08:13 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:08:13 server2 snmpd[800]: last message repeated 2 times
Feb  1 22:08:13 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:08:25 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:08:25 server2 snmpd[800]: last message repeated 6 times
Feb  1 22:09:05 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:09:35 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:09:57 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:10:13 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:10:13 server2 snmpd[800]: last message repeated 3 times
Feb  1 22:10:43 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:10:43 server2 snmpd[800]: last message repeated 5 times
Feb  1 22:11:05 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:11:35 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]
Feb  1 22:11:57 server2 snmpd[800]: Connection from UDP: [10.10.10.32]:50618->[10.10.10.33]

My goal is to get only some time specific part of the log. Eks. last 24 hours
# 10  
Old 02-01-2011

nawk -v from='Feb 1 22:09:35' -v to='Feb 1 22:11:35' -f jot.awk myLogFile


jot.awk:
Code:
function convDate(m,d,t)
{
  gsub(":","",t)
  return sprintf("%0.2d%0.2d%s",monA[toupper(m)],d,t)
}

BEGIN {
   mon="JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC"
   monN=split(mon, monA, "|");
   for(i=1; i<=monN; i++) {
     monA[monA[i]]=i;
     delete monA[i];
   }

   split(from,fromA,FS)
   split(to,toA,FS)
   fromI=int(convDate(fromA[1], fromA[2], fromA[3]))
   toI=int(convDate(toA[1], toA[2], toA[3]))
}
{
   d=int(convDate($1,$2,$3))
   if (d>=fromI && d<=toI) print
}

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Preserve leading white space

I have about 350 programs in which I have to add 2 lines; one before and one after a specfic line. The following script does the job except that I lose the indentation. #!/usr/bin/bash while read line ... (8 Replies)
Discussion started by: jgt
8 Replies

2. Shell Programming and Scripting

How to preserve the value of a variable from being overwritten?

Hi All, I am new new to unix.com, I have a question related to shell scripting. We have a Oracle database backup shell script, which can be used for taking full, incremental & archive log backup based on the parameters passed. Within the script we export a variable as export... (5 Replies)
Discussion started by: veeresh_15
5 Replies

3. Shell Programming and Scripting

How to preserve spaces in input fields with awk?

I'm trying to do something pretty simple but its appears more complicated than expected... I've lines in a text file, separated by the comma and that I want to output to another file, without the first field. Input file: file1,item, 12345678 file2,item, 12345678 file2,item, ... (8 Replies)
Discussion started by: Armoric
8 Replies

4. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

5. Shell Programming and Scripting

awk - How to preserve whitespace?

Given a file: # configuration file for newsyslog # $FreeBSD: /repoman/r/ncvs/src/etc/newsyslog.conf,v 1.50 2005/03/02 00:40:55 brooks Exp $ # # Entries which do not specify the '/pid_file' field will cause the # syslogd process to be signalled when that log file is rotated. This # action... (10 Replies)
Discussion started by: jnojr
10 Replies

6. Shell Programming and Scripting

Preserve trailing whitespace in variable

Hello, I wondering how I can echo a string without having the trailing whitespace removed. For example I have a string str="TESTING123 " that I need to hash using sha1. I get the correct answer when I run the line below from the terminal $ echo -n "TESTING123 " | openssl sha1... (3 Replies)
Discussion started by: colinireland
3 Replies

7. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

8. UNIX for Dummies Questions & Answers

awk for variable with a space

Hi experts, why does $ echo "one two three" | awk '{$3="my tree"; print $0}' one two my tree work, but $ var="my tree" $ echo "one two three" | awk '{$3="'$var'"; print $0}' awk: {$3="my awk: ^ unterminated string does not work? How can the variable tha contains a space be... (2 Replies)
Discussion started by: GermanicGalore
2 Replies

9. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

10. Shell Programming and Scripting

How to preserve space while concatenating strings? (KSH)

I have these str1=$(echo "This is string one with spaces \n This is also my sentence 1") When I echo $str1, it displays the new line character properly. Now I have another new variable say str2. I want to concatenate in this way.. str1 + newline character + and then str2. That's I... (3 Replies)
Discussion started by: dahlia84
3 Replies
Login or Register to Ask a Question