Debug an Awk Script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Computer Debug an Awk Script

I would like to extract the following fields from the text file attached. I copied the contents from a pdf file and pasted them into the text file so I can use awk to extract them. The layout is as listed below.

name1,name2,name3,name4,Title,designation,nationality,dob,
national identication,address,listed On,Other information


The problem is if I remove the comments in the script I seem to get nothing in the out put file. Please help I'm new to awk programming and its baffling me. And some of the information that I'm extracting is in the wrong columns. What am I doing wrong ? Please advice. I'm new to this language

1) This is the script

Code:
  BEGIN { print "name1,name2,name3,name4,Title,designation,nationality,dob,national identication,address,listed On,Other information";Title=""; dob=""; nationality="";
  }
/*Name(:?)/ {

    if (Title != "" )
    {
      printf ",%s",Title;
      Title=" ";
    }
    if (designation != "" )
    {
      printf ",%s",designation;
      designation=" ";
    }
    if (nationality != "" )
    {
      printf ",%s",nationality;
      nationality=" ";
    }
    if (dob != "" )
    {
      printf ",%s\n",dob;
      dob=" ";
    }
    line = $0;
    gsub(/[[:digit:]]+:/,"",line);
    gsub(/[[:digit:]]+\./,"",line);
    sub(/*Name:/,"",line);
    sub(/*Name/,"",line);
    split(line,names);
    x = 1;
    while (x<=3)
    {
      printf "%s,", names[x];
      x++;
    }
    printf "%s", names[x];
    name1 = $4;
    name2 = $6;
    name3=$8;
    name4=$10;
}
##--> Add title field
/Title:/ {
  line=$0;
  idx1 = index(line,"Title:");
  idx2 = index(line,"Designation:");
  if (idx2==0)
  {
    idx2 = length(line)+1;
  }
  Title = substr(line, idx1+length("Title:"), idx2 - idx1 - length("Title:") );
  sub(",",";",Title)
}

##--> Add Designation
/Designation:/ {
  line=$0;
  idx1 = index(line,"Designation:");
  idx2 = index(line,"DOB:");
  if (idx2==0)
  {
    idx2 = length(line)+1;
  }
  designation = substr(line, idx1+length("Designation:"), idx2 - idx1 - length("Designation:") );
  sub(",",";",designation)
}

/*Nationality: / {
  line = $0;
  idx1 = index(line,"*Nationality: ");
  idx2 = index(line,"Passport");
  nationality = substr(line, idx1+length("*Nationality: "), idx2 - idx1 - length("*Nationality: ") );
  sub(",",";",nationality)
}
/DOB:/ {
  line=$0;
  idx1 = index(line,"DOB:");
  idx2 = index(line,"POB");
  if (idx2==0)
  {
    idx2 = length(line)+1;
  }
  dob = substr(line, idx1+length("DOB:"), idx2 - idx1 - length("DOB:") );
  sub(",",";",dob)
}
##/National identification no.:/ {
  ##line=$0;
  ##idx1 = index(line,"National identification no.:");
  ##idx2 = index(line,"Address");
  ##if (idx2==0)
  ##{
  ##  idx2 = length(line)+1;
  ##}
  ##dob = substr(line, idx1+length("National identification no.:"), idx2 - idx1 - length("National identification no.:") );
  ##sub(",",";",National identification no)
##}

##/Title:/ {context="title"; printf "%s:",context}


Last edited by vino; 10-01-2007 at 08:08 AM.. Reason: Added code tags.
# 2  
Have added More fields (New layout)

name1,name2,name3,name4,Title,designation,nationality,dob,POB,
Good quality a.k.a ,Low quality a.k.a,national identication,address,listed On,Other information
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #568
Difficulty: Easy
A while loop is a type of loop that requires an initial and a terminating condition.
True or False?

10 More Discussions You Might Find Interesting

1. Ubuntu

Debug script

How can I debug this script? I want to know what it is doing or not doing? #!/bin/bash # # if ; then # Do the thing you want before suspend here echo "we are suspending." > /tmp/systemd_suspend_test.txt elif ; then # Do the thing you want after resume here echo "and we are... (21 Replies)
Discussion started by: drew77
21 Replies

2. Shell Programming and Scripting

[Solved] How to debug awk script?

how can i view what variables are stored upon the excution of an awk script. something equivalent to shell eg. sh -vx "script_file" many thanks in advance! (1 Reply)
Discussion started by: Apollo
1 Replies

3. Shell Programming and Scripting

Debug perl script

Hi, I am trying to get squid up and running using a redirector process, and every time I try to load a web page, squid fails miserably. Can some one with perl and squid knowledge take a look at these codes and tell if something is wrong here. #!/usr/bin/perl # $| = 1; @endings = qw/... (0 Replies)
Discussion started by: jamie_123
0 Replies

4. Shell Programming and Scripting

Debug script already running

Hello, is it possible to debug a script that is already running "on-demand"? Somehow a command like "set -x" but once you've executed the script. And another one to disable debugging? Thank you (0 Replies)
Discussion started by: asanchez
0 Replies

5. Shell Programming and Scripting

Cant debug shell script

Hi I am relatively new in shell scripting Below is the code which i developed but for some reason, it keeps giving me error: /apps/bss/BatchProg/at1/batch/scripts/ksh/TBATLC02.ksh: syntax error at line 41 : `then' unmatched #!/usr/bin/ksh... (4 Replies)
Discussion started by: scripting_newbe
4 Replies

6. Shell Programming and Scripting

Help with debug the script

Hi, I have this script, searches and sets variables, then searches and sets more variables from multiple files. I'd need to debug it a bit. #!/bin/bash egrep $1 `find | grep MAGT` >/tmp/resRA-$$ thread=`sed -n '/{0x/ {s/^.*{0x\(*\).*/\1/p;q}' /tmp/resRA-$$` tag=`sed -n '/Tag=/... (5 Replies)
Discussion started by: Vitoriung
5 Replies

7. Shell Programming and Scripting

Problem with the script, help me debug

Hi, When i run the script ./script.sh sun, this give me no output, it should give me the list of file. If i run the script without the argument it should send me echo inside usage(). What is the problem? please help -Adsi #!/bin/sh ROOT_PATH=/net/icebox/vol/local_images/spins... (2 Replies)
Discussion started by: asirohi
2 Replies

8. Shell Programming and Scripting

Please help to debug a small shell script (maybe AWK problem)?

Hi Buddies, The following is shell scripts which was borrowed from linux box for load average check. it runs good. (this structure is simple, when load average is too high, it will send alert to user) #!/usr/bin/ksh # Set threshold for 1, 5 and 15 minture load avarage # configured for... (4 Replies)
Discussion started by: GreatJerry
4 Replies

9. Shell Programming and Scripting

debug this script

echo "input time in hhmmss" read $st h=`echo $st | cut -c1-2` min=`echo $st | cut -c3-4` s=`echo $st | cut -c5-6` echo "input time in hhmmss" read $end h1=`echo $end | cut -c1-2` min1=`echo $end | cut -c3-4` s1=`echo $end | cut -c5-6` x= `expr $h /* 60 + $min` y= `expr $h1 /* 60 +... (8 Replies)
Discussion started by: abhishek27
8 Replies

10. Shell Programming and Scripting

How to debug the awk script

Hi, How can I debug an awk script? I know that set -x can be used to debug a script. But this will not suite for awk scripts. Can anyone help me? Thanks in advance, Chella (2 Replies)
Discussion started by: chella
2 Replies

Featured Tech Videos