Explanation of NAWK Code


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Explanation of NAWK Code
# 1  
Old 10-25-2010
Explanation of NAWK Code

Hi all,

I need your help in understanding the meaning and syntax of the below nawk line.
Here is an extract of a script which I use daily and works well. The script extracts the hostnames and messages within a syslog file. I would also like to extract the message time in the 3rd column by updating the nawk line, but need your help to do it:

Code:
grep $messages syslog.`date +%d%m%y`.log | \
nawk -F '%' '{ split($1,a," "); print a[4], "%"$2 }' | \
sort -k3.2,3 -k2,2 -k1nr,1 | \

Regards, Wynford



---------- Post updated at 03:11 PM ---------- Previous update was at 02:37 PM ----------




I've now found out how to adapt the nawk line to show the time column, see below:

Code:
nawk -F '%' '{ split($1,a," "); print a[3], b[4], "%"$2 }'

I still now need an explanation of the whole line.

Wynford
# 2  
Old 10-25-2010
-F '%' - this defines % as the primary field separator.

split($1, a, " ") - using field $1 (field #1 ) create an array that is split up using space(s) as the separator, creating subfields out of $1.

print the 3rd and 4th subfield of $1, print the "%" character, then primary field #2.

If you are doing sysadmin work you have to know shell and awk. Period.

Seriously consider getting a copy of Dougherty & Robbins 'sed & awk' - you can get an ebook for a lot less than paper.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-25-2010
Thanks Jim, much appreciated Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Explanation of Nawk command

Hi Folks, I am struggling to understand nawk command which was used by another developer. Can you please explain what each character or string is doing here below: if ; then (3 Replies)
Discussion started by: kirans.229
3 Replies

2. Shell Programming and Scripting

ksh code explanation

Hi. Can somebody please explain the following lines of KSH code for me? The code checks all sub directories in a specific location which are numbered (E.g. test_01, test_02 ... etc.), then finds the one with highest number and extracts that number from the dir name into the variable num. I'd just... (9 Replies)
Discussion started by: user052009
9 Replies

3. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

4. Homework & Coursework Questions

Minix 3 Source Code Organisation Explanation

1. I've been asked to provide a detailed description on how Minix's source code is organised. This may sound slightly simple, but what exactly am I to refer to when explaining? Any documents that would provide assistance to understand whats being asked? Thanks 2. Not applicable: 3.... (1 Reply)
Discussion started by: Menace12
1 Replies

5. UNIX for Dummies Questions & Answers

Code explanation?

I need someone to tell me how exactly works the following code αfter /etc/passwd :eek:: cat /etc/passwd|grep "^:"|sed '1,$s/^\(*\):*:\(*\):.*$/ \1 \2 /'|sort -nrk3 -t:I want a good explanation to understand the code please (4 Replies)
Discussion started by: kotsos13
4 Replies

6. Shell Programming and Scripting

code explanation

Can you please explain the following code plz? my_cd=' ' while getopts :e: OPTION; do case "$OPTION" in e) my_cd ="$OPTARG";; esac done if ; then echo " >>> ERROR - I am wrong" echo " >>> ERROR - Hello" exit 99 fi What I don't understand is what is OPTION or... (3 Replies)
Discussion started by: RubinPat
3 Replies

7. Homework & Coursework Questions

Please help with the following fork code..with complete explanation

I have the following piece of codes. Please explain it to me in great detail how are these codes working. 1. #include <stdio.h> int main(){ int x; x=0; while (x<2 && fork()){ if (!fork()) execlp("echo","x++","x",0); x++; system("echo x+x"); } } 2. #include <stdio.h> int i;... (1 Reply)
Discussion started by: prakashabii
1 Replies

8. Shell Programming and Scripting

nawk code explanation

Hi I have a line/command which greps certain pattern () from the file and 22 lines AFTER that: nawk '/\/{c=22}c&&c--' input_file Can somebody explain the coding in this command (what " c&&c-- " does)? and how to modify this command to output 22 lines BEFORE this pattern () Thanks a lot ... (3 Replies)
Discussion started by: aoussenko
3 Replies

9. Shell Programming and Scripting

AWK deifnition and some small code explanation

Could someone give me a quick simple explanation for the AWK command. And also help me to explain the code i have made. I have made some general comments about it myself. I was wondering if people could help me with the rest: awk -F'' 'END { fmt = "%-20s\t%s\t%s\n" ... (0 Replies)
Discussion started by: amatuer_lee_3
0 Replies

10. Shell Programming and Scripting

Need explanation for the syntax(code)

Hi I am new to shell script programming... want to know the process of the following: if then echo "$0: missing argument for option(s) :$MISSINGOPTARG" echo "usage" $USAGE" exit 1 fi (1 Reply)
Discussion started by: chandhar
1 Replies
Login or Register to Ask a Question