The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 10-01-2007
mboro mboro is offline
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, 9 views)

Last edited by vino; 10-01-2007 at 06:08 AM.. Reason: Added code tags.