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 and shell scripting languages 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 3 09-07-2008 11:31 AM
How to debug the awk script chella Shell Programming and Scripting 2 11-08-2007 07:51 PM
debug aix 4.3 itik AIX 2 10-29-2007 11:56 AM
how to debug ramneek High Level Programming 1 09-19-2005 07:35 AM
disk space script debug - posted before bryan Shell Programming and Scripting 3 04-28-2005 08:50 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #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, 10 views)

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0