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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Altering address data in .csv files... 2

Status
Not open for further replies.

admoore

IS-IT--Management
May 17, 2002
224
US
First,

Thanks for all of the excellent, accurate advice I've been given here...

Finally, I have one (hopefully) last issue to resolve...

I have a comma delimited file w/ customer data that needs to be converted to "proximity" type data, for example:

firstname,lastname,123 Anywhere street,Hometown,STATE,zip
firstname,lastname,456 Anywhere street,Hometown,STATE,zip
firstname,lastname,789 Anywhere street,Hometown,STATE,zip


Would become:

120 Anywhere street,Hometown,STATE,zip
121 Anywhere street,Hometown,STATE,zip
122 Anywhere street,Hometown,STATE,zip
124 Anywhere street,Hometown,STATE,zip
125 Anywhere street,Hometown,STATE,zip
126 Anywhere street,Hometown,STATE,zip
453 Anywhere street,Hometown,STATE,zip
454 Anywhere street,Hometown,STATE,zip
455 Anywhere street,Hometown,STATE,zip
457 Anywhere street,Hometown,STATE,zip
458 Anywhere street,Hometown,STATE,zip
459 Anywhere street,Hometown,STATE,zip
786 Anywhere street,Hometown,STATE,zip
787 Anywhere street,Hometown,STATE,zip
788 Anywhere street,Hometown,STATE,zip
790 Anywhere street,Hometown,STATE,zip
791 Anywhere street,Hometown,STATE,zip
792 Anywhere street,Hometown,STATE,zip


That is the numeric portion of the address would be decrimented and incrimented by three, and output without the original numerical address. The name fields are unimprtant for this aaplication and will be discarded.

As always TIA for any help here...

-Allen
 
My own solution was...
Code:
awk 'BEGIN{
  FS=",";OFS=",";
}
{
   numpref=split($3,addr, " ");
   print addr[1]-3" "addr[2]" "addr[3]" "addr[4]" "addr[5]" "addr[6],$4,$5,$6,$17;
   print addr[1]-2" "addr[2]" "addr[3]" "addr[4]" "addr[5]" "addr[6],$4,$5,$6,$17;
   print addr[1]-1" "addr[2]" "addr[3]" "addr[4]" "addr[5]" "addr[6],$4,$5,$6,$17;
   print addr[1]+1" "addr[2]" "addr[3]" "addr[4]" "addr[5]" "addr[6],$4,$5,$6,$17;
   print addr[1]+2" "addr[2]" "addr[3]" "addr[4]" "addr[5]" "addr[6],$4,$5,$6,$17;
   print addr[1]+3" "addr[2]" "addr[3]" "addr[4]" "addr[5]" "addr[6],$4,$5,$6,$17;

}'
 
Hi Allen,

Here is another way!

myfile.awk follows:

{
FS = ","
$1 = ""
$2 = ""
fld3 = $3
text = substr ($3,4)

for (j = 3; j >= 1; j--) {
$3 = (fld3 - j)text
for (a = 3; a < NF; a++)
printf(&quot;%s,&quot;, $a)
printf(&quot;%s\n&quot;, $NF)
}

for (k = 1; k <= 3; k++) {
$3 = (fld3 + k)text
for (b = 3; b < NF; b++)
printf(&quot;%s,&quot;, $b)
printf(&quot;%s\n&quot;, $NF)
}
}


Launch from command line thus:

nawk -f myfile.awk inputfile > outputfile

Hope this helps you.


flogrr
flogr@yahoo.com

 
i really don't understand the obstinacy to make simple things SOOOO complex.

if your post is corret, you will cut
firstname,lastname,
out of
firstname,lastname,789 Anywhere street,Hometown,STATE,zip
to get
789 Anywhere street,Hometown,STATE,zip

cut it !!!!

cut -d, -f2- input >output

sed can better manipulate it, but is a littler complex:
this is the same like cut:
sed -e 's/\(.*\),\(.*\),\(.*\),\(.*\),\(.*\),\(.*\)/\3,\4,\5\6/'
this prints:
789 Anywhere street,Hometown,STATE,zip,lastname,firstname
sed -e 's/\(.*\),\(.*\),\(.*\),\(.*\),\(.*\),\(.*\)/\3,\4,\5\6,\2,\12/'

-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
sorry for typo: \12 should be \1 -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
It was intended to discard the name info, as declared in the original post, as the name info is only valid for the original street address, not important in this application...

Thanks...
 
BTW,

How would I best deal w/ a .csv file if a text identifier of &quot; was used as follows...

Code:
&quot;firstname&quot;,&quot;lastname&quot;,&quot;123 Anywhere street&quot;,&quot;Hometown&quot;,&quot;STATE&quot;,&quot;zip&quot;

TIA!
 
exactly the same was as long you have NOT:

&quot;first,name&quot;,&quot;last,name&quot;,&quot;123 ...&quot;

nota the kommas included by &quot;&quot;
in this case use cut

cut -d\&quot; -f1,3,5,7,9,11 -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
sure sed do it also:
's/&quot;\(.*\)&quot;,&quot;\(.*\)&quot;,&quot;\(.*\)&quot;,&quot;\(.*\)&quot;,&quot;\(.*\)&quot;,&quot;\(.*\)&quot;/\3,\4,\5\6,\2,\1/'

don't forget, you can use exactly this cmd in /bin/vi
change the leading ' by: :%
change the last one by: g
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top