The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can debug our UNIX shell script? psiva_arul UNIX for Advanced & Expert Users 2 05-20-2008 08:54 AM
How to debug the awk script chella Shell Programming and Scripting 2 11-08-2007 03:51 PM
debug aix 4.3 itik AIX 2 10-29-2007 07:56 AM
how to debug ramneek High Level Programming 1 09-19-2005 03:35 AM
disk space script debug - posted before bryan Shell Programming and Scripting 3 04-28-2005 04:50 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-01-2007
Registered User
 

Join Date: Oct 2007
Posts: 2
Talking 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}
Attached Files
File Type: zip AwkScripts.zip (2.4 KB, 4 views)

Last edited by vino; 10-01-2007 at 03:08 AM. Reason: Added code tags.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-01-2007
Registered User
 

Join Date: Oct 2007
Posts: 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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0