Visit Our UNIX and Linux User Community


Explain this AWK script plz


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explain this AWK script plz
# 8  
Old 08-04-2009
The minimum to understand an awk script (Extract from awk man pages) :
Quote:
Files are processed in the following way:

1. The awk command scans its instructions and executes any actions specified
to occur before the input file is read.

2. One record is read from the input file.

3. The record is compared against each pattern specified by the awk command's
instructions.

4. If the record matches a pattern, any actions associated with that pattern
are performed on the record.

5. After the record is compared to each pattern, and all specified actions are
performed, the next record is read from input; the process is repeated
until all records are read from the input file.

6. If multiple input files have been specified, the next file is then opened
and the process repeated until all input files have been read.

7. After the last record in the last file is read, the awk command executes
any instructions specified to occur after the input processing.


The awk command programming language consists of statements in the form:

Pattern { Action }

If a record matches the specified pattern, or contains a field which matches the
pattern, the associated action is then performed. A pattern can be specified
without an action, in which case the entire line containing the pattern is
written to standard output. An action specified without a pattern is performed
for every input record.
The following code :
Code:
/Targ Rowid=/ { 
   Rejected_count++ ;
   Get_detail++;
   printf "#%d => %s\n", Rejected_count, Ora_error;
   next;      # Proceeds with the next
input record.

}

/^)/ && Get_detail {
   print Rejected_count,Values_detail > Outfile; 
   Values_detail = "";
   Get_detail = 0;
   next;
}

Get_detail {
   sub(/\(.*\):/,"=");
   Values_detail = Values_detail $0 "|"
}

Can be rewritten like that :
Code:
$0 ~ /Targ Rowid=/ { 
   Rejected_count = Rejected_count + 1 ;
   Get_detail     = 1;
   printf "#%d => %s\n", Rejected_count, Ora_error;
   next;
}

if $0 /^)/ && Get_detail != 0 {
   print Rejected_count,Values_detail > Outfile; 
   Values_detail = "";
   Get_detail = 0;
   next;
}

Get_detail != 0 {
   sub(/\(.*\):/,"=");
   Values_detail = Values_detail $0 "|"
}

Get_detail isn't a function, it's a variable used as a boolean (0=false, other values=true).

Jean-Pierre.
# 9  
Old 08-04-2009
i assure u , i ll sleep tonight well. i got cleared with control flow of the script. : ) Thanks you very much for explained me line by line. really thanku
[code]
Open for Open for
File Records Read Write
--------------- ------- -------- --------
Inputfile ? 5004
ora_occ.txt 5000 5000
targ_occ.txt 5000 5000
bracket_occ.txt 5000 5000
ora_error_deatail 1 5000 5000
data_block.txt 46 5000 5000
--------------- ------- -------- --------
Total 30004 10000
---------
[code]

how could get this detail. is theere any command to get details of I/O for paricular file. Plz suggest me frnd
# 10  
Old 08-04-2009
Quote:
Originally Posted by Gopal_Engg
i assure u , i ll sleep tonight well. i got cleared with control flow of the script. : ) Thanks you very much for explained me line by line. really thanku
Code:
Open for Open for
File Records Read Write
--------------- ------- -------- --------
Inputfile ? 5004
ora_occ.txt 5000 5000
targ_occ.txt 5000 5000
bracket_occ.txt 5000 5000
ora_error_deatail 1 5000 5000
data_block.txt 46 5000 5000 
--------------- ------- -------- --------
Total 30004 10000
---------



how could get this detail. is theere any command to get details of I/O for paricular file. Plz suggest me frnd
There is no command to get this kind of detail.
This detail is the result of my analyse of your script.

Jean-Pierre.

Previous Thread | Next Thread
Test Your Knowledge in Computers #506
Difficulty: Easy
A flowchart is a visual representation of an algorithm or process consisting of boxes or 'nodes' connected by directed edges.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Plz explain the process of this code....

Hi Unix Gurus, Please explain the processing done by the code mentioned below: for (i=1;i<=59;i++) { sub(/ *$/,"", $i) } newrec_key = $2 new_line = $0 if (empty_oldfile==1) { #newly added record ++num_add print "Added key: ", newrec_key > LogFile #print out... (4 Replies)
Discussion started by: ustechie
4 Replies

3. Shell Programming and Scripting

plz explain the execution flow

CODE file=/tmp/rap54ibs2sap.txt trap "rm $file; exit" 0 1 2 3 15 trap rm $file execution result trap -- 'rm /tmp/rap54ibs2sap.txt; exit' EXIT trap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGHUP trap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGINT trap -- 'rm /tmp/rap54ibs2sap.txt;... (1 Reply)
Discussion started by: rrd1986
1 Replies

4. Shell Programming and Scripting

explain plz

can u explain this step by step........plz...will it do the same for I in *.tar.gz; do A=`basename $I .tar.gz` mkdir $A cp marking-guide ${A}/$A cd $A gunzip -c ../$I | tar xf - cd.. done thnx __________________ (2 Replies)
Discussion started by: avikal
2 Replies

5. Shell Programming and Scripting

perl doubt plz explain....

hi all i wrote a shell script which uses perl script my code is : >cat filename | while read i >do >perl -e 'require "/home/scripts/abc.pl" ; abc("$i")' >done perl script used will simply check syntax of Cobol programs but it didn't work for me so i asked my colleague he suggested... (1 Reply)
Discussion started by: zedex
1 Replies

6. Shell Programming and Scripting

plz help in writing awk script

hi buddies pls help in this matter i have file like this input file -------------------------- (PARTITION PARTITION_1 VALUES LESS THAN (101, 16383 ) TABLESPACE PART_1 ,PARTITION PARTITION_2 VALUES LESS THAN (101, 32766 ) TABLESPACE PART_2 ,PARTITION PARTITION_3 VALUES LESS THAN (101,... (3 Replies)
Discussion started by: LAKSHMI NARAYAN
3 Replies

7. Shell Programming and Scripting

help in writing awk script, plz urgent

I have a file like this I have to I have input file this , I want to give the out put in the below input file (NARAYANA 1 ENDING AT (100, 16383) ,NARAYANA 2 ENDING AT (100, 32766) ,NARAYANA 3 ENDING AT (100, 49149) ,NARAYANA 4 ENDING AT (100, 65535) ,NARAYANA 5... (8 Replies)
Discussion started by: LAKSHMI NARAYAN
8 Replies

8. UNIX for Dummies Questions & Answers

explain command plz

echo -n "1. Updating Password Policy in OID..." | tee -a $logfile set ldap_v1 = `ldapsearch -b "" -h $oid_host -p $oid_port -D "cn=orcladmin" -w $admin_pwd -s sub "cn=PwdPolicyEntry" dn | head -1` echo "dn:$ldap_v1" > ldap1.out echo "changetype:modify" >> ldap1.out echo... (2 Replies)
Discussion started by: maoro
2 Replies

9. UNIX for Advanced & Expert Users

Can anyone explain plz

HI, Can anyone explain to me how does the following command work - > current_dir=`cd \`/usr/bin/dirname $0\` && pwd` Regards, Ranga (3 Replies)
Discussion started by: r_W213
3 Replies

10. 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

Featured Tech Videos