Please explain what this Awk code is doing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please explain what this Awk code is doing
# 1  
Old 10-31-2011
Please explain what this Awk code is doing

Hi Guys,

Please help me, I am new to programming and I don’t understand what some parts of this code are doing. I have comments on the parts I know, please help if my understanding of the code is not correct and also help with parts with questions.
Code:
  awk '
      {
          gsub( ">", "" );        #replace the > with space
          gsub( " ", "~" );          #replace the  space with match 
          gsub( "<", " " );          #replace the < with space
   
   
          for( i = 1; i <= NF; i++ )          #For loop which sets i to 1 and i is less than or equal to number  of fields and I increases by 1 each time the loop run. 
   
          {
              if( split( $(i), a, "=" ) == 2 )  #split the string i into array a on the regular expression. 
  What is ==2 doing 
              {
                  gsub(  "\"", "", a[2] ); #replace \ with space 
  What is a[2] doing , is this array index 2 if so why index 2
                  gsub(  "~", " ", a[2] );
                  values[a[1]] = a[2];
  What is this doing values [a[1]]=a[2];             
  }
          }
   
          gcount[values["Gender"]]++;         # collect counts
          acount[values["Age"]]++;
   
          printf( "%s %s %s %s\n", values["NAME"], values["Age"], values["D.O.B"], values["Gender"] );
      }
   
      END {
          printf( "\nAge Count" );
          for( x in acount ) #for loop make the x as index for array acount 
              printf( "%s %d\n", x, acount[x] );
  What is this print doing? It is printing the values in x but how. 
          printf( "\nGender Count:\n" );
          for( x in gcount )
              printf( "%s %d\n", x, gcount[x] );
      }
  ' input_file

Example of the xml message is.
Code:
  [date+time], message=[DATA= “<?xml version=”1.0?”><data changeMsg><NAME=”John Smith”><Age=”23”><D.O.B=”11-10-1988”> <Gender=”Male”>”

Sorry guys I know I am asking a lot but any help would be greatly appreciated?

Thank you all

Last edited by Franklin52; 11-01-2011 at 04:10 AM.. Reason: Please use code tags, thank you
# 2  
Old 11-01-2011
What is ==2 doing
== 2 is matching the result of the split. Hes doing 2 things on one line (like he should)

Think of like like this:

split (blablabla)
if split result equals 2

What is a[2] doing , is this array index 2 if so why index 2
Does the gsub for the value in a[2]

He's probably removing the " from the value

values[a[1]] = a[2]
Building the values array so a[1] will be the key to a[2]

What is a[2] doing , is this array index 2 if so why index 2
printing both values of x and acount[x] first one as a string and the other one as decimal.

Hope it helps.
This User Gave Thanks to maverick72 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can someone please explain why we need to set ORS in below awk code?

Question: Write a command to print the fields in a text file in reverse order? awk 'BEGIN {ORS=""} { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename I was thinking it should be (what is the need to set ORS="" ? )- awk 'BEGIN { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Explain awk

I have 2 files recevied abc def ghi totallist abc 123 jasdhfaj def 345 fjdgkfsfh ghi 567 dfjdhdhfj jkl 678 djkahfdjshdf xyz 984 jdfdhfhdh myOutputFile jkl 678 djkahfdjshdf xyz 984 jdfdhfhdh I used this command for the output : awk 'FNR==NR {f1;next} !($1 in f1)' recevied... (2 Replies)
Discussion started by: nani1984
2 Replies

3. Shell Programming and Scripting

Explain this awk

found this handy one liner in another thread which is closed, it does what i need but im trying to understand it. it basically matches the field that contains the value v and prints its position awk -F, '{for(i=1;i<=NF;i++)if($i==v)print i}' v=yourfield inputfile my understanding is assign... (3 Replies)
Discussion started by: jack.bauer
3 Replies

4. Shell Programming and Scripting

can you explain this sed code?

can anyone please explain this code? sed ':a;N;$!ba;s/]\n//g' file it replaces lines ending with "]" and concatenates with the next line so that line1] line2 becomes line1line2 i don't understand this part: :a;N;$!ba; I have noted that I can replace "a" with any letter: ... (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

Please explain what this code is doing

Hi, Pls explain me what the below code is doing. specially meaning if -a while calling test function- case $1 in 1) beg_dt=01; end_dt=07 ;; 2) beg_dt=08; end_dt=14 ;; 3) beg_dt=15; end_dt=21 ;; 4) beg_dt=22; end_dt=28 ;; 5) beg_dt=29; end_dt=31 ;; esac test \( `date +%w` -eq $2 -a... (3 Replies)
Discussion started by: sendtoshailesh
3 Replies

6. Programming

Explain a block of code:

Hi, I have this block in a code and I need to know it's meaning: bool selectEvents = config.get("selectEvents",false); if (selectEvents) { ifstream in("events"); while (in) { int run, evt; in >> run >> evt; if (in.eof()) break; ... (5 Replies)
Discussion started by: eager2no
5 Replies

7. Shell Programming and Scripting

Can any one explain what this code will do

ccc_con,CCC_CON,0 Above is the input for this code #!/usr/bin/bash my_path=`dirname $0` T_table=$1 S_table=$2 P_table=$3 #Star new code while read ${my_path}/arch_table_list.txt { awk -F "," '{print $1}' ${my_path}/arch_table_list.txt ${S_table} awk -F "," '{print... (1 Reply)
Discussion started by: scorp_rahul23
1 Replies

8. UNIX for Advanced & Expert Users

explain the code

Hi , Can anyone explains what does the below highlighted statements means: # Set environment variables . ${0%/*}/wrkenv.sh jobName_sh=${0##*/} jobName=${jobName_sh%.*} Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

9. Shell Programming and Scripting

Explain awk

Hi, I found this command in this forum, but, couldnt understand much from it. could any one help me understand that??? the commands are : awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2 awk '{sub(/~ */,x);printf $0(/\|$/?ORS:x)}' awk '{sub(/~ */,x);sub(/\|$/, "|\n")}8'... (4 Replies)
Discussion started by: hitmansilentass
4 Replies

10. UNIX for Dummies Questions & Answers

could someone explain this code

hey peeps could someone explain what this part of the code means: 'if echo $* | grep -q' $i '|| thanks tHe_nEw_GuY (2 Replies)
Discussion started by: the_new_guy
2 Replies
Login or Register to Ask a Question