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!

Adding reference number to field in CSV file 2

Status
Not open for further replies.

ianicr

IS-IT--Management
Nov 4, 2003
230
GB
Is there a way to add a ref no to a csv file in a certain field with awk? I need to add 0000000003 to field 6 then 0000000010 to the next, then 0000000017 to the next etc.
Thanks
 
Give us an example of input and output file please.

Jean Pierre.
 
Input file:
"Mr","T","Smith","1 New Street","Birmingham",""
"Mr","R","Smith","3 New Street","Birmingham",""

Output File
"Mr","T","Smith","1 New Street","Birmingham","00000000003"
"Mr","R","Smith","3 New Street","Birmingham","00000000010"

It doesn't really matter about the leading zeros but that would be nice! Doesn't really matter about the quotes either as I can tr them out.
 
Try something like ths:
Code:
awk -F, 'BEGIN{i=3}
$7=="\"\""{$7=sprintf("%010d",i);i+=7}
{print}
' /path/to/input >output

Hope This Help
PH.
 
On my aix box, the output field separator is not affected by the -F option, I must add the statement : OFS=","

awk -F, 'BEGIN{OFS="," ;i=3}
$6=="\"\""{$6=sprintf("%010d",i);i+=7}
{print}
' /path/to/input >output

Jean Pierre.
 
Good catch Jean-Pierre, for the OFS issue and the $6.
ianicr, sorry for the typo.
 
Thanks to both of you. That worked great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top