Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

duplicating data in conversion script

Status
Not open for further replies.

admoore

IS-IT--Management
May 17, 2002
224
US
I use the following script to re-format data by separating the area code from the phone number in the file, and providing a default area-code if there isn't one in the original record. The only problem is, that upon output, I get two lines per record, instead of one. Any help would be appreciated, here is my code:

Code:
BEGIN {
	  FS=",";
	  OFS=",";
	  print "CustomerID,FirstName,LastName,TimeZone,AreaCode,PhoneNumber";
	  Z="MST";  #time zone
	       
	}

  NR>1{
	  gsub ("-","");
	  gsub ("\"","");
	  
        
	  if(length($5)==10)
		{ # split phone no w area_code
		  area_code = substr($5,1,3);
              phone = substr($5,4,7);
		}
	  else
		{		  
		  phone=$5;
		  area_code = 623;
		}

	  print $1,$3,$4,Z,area_code,phone;
	}

Thanks in advance for any ideas...

-Allen
 
Oops, the problem was in the way I was calling the script, not the script itself... I inadvertantly typed the input file name 2x on the command line...

Sorry for the inconvenience,

-A
 
Your code looks as though it should work. Please supply
some sample data.

The effect of [tt] NR>1{ [/tt] is to skip the first line of the file. Is this what you want?

Unlike Perl and C, Awk doesn't require a ";" after each
statement; a semicolon is used to separate two statements
on the same line.

Code:
BEGIN {
      FS=","
      OFS=","
      print "CustomerID,FirstName,LastName," \
            "TimeZone,AreaCode,PhoneNumber"
      Z="MST"  #time zone
}


NR > 1 {
      # Unless assigning to variable,
      # delimit reg.exp. with /.
      gsub(/-/,"")
      gsub(/"/,"")
        
      if(length($5)==10)
        { # split phone no. w area_code
          area_code = substr($5,1,3)
          # To get rest of string, omit 3rd argument.
          phone = substr($5,4)
        }
      else
        {          
          phone=$5
          area_code = 623
        }

      print $1,$3,$4,Z,area_code,phone
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top