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!

Awk to Sort and Remove Numbers 2

Status
Not open for further replies.

beaster

Technical User
Aug 20, 2001
225
US
I have a text file which is quite large. The file looks similiar like below:

Cast Iron 461
Copper 627
Gold 53
Pewter 554
Wrought Iron 103
Other 792
Mirrors 377
Picture Frames 850
Toleware 732
Woodenware 1711
Other 2736
Ethnographic 3236
African 1341
Latin American 441
Native American 742

I need help for an awk script that will read line by line, get rid of any numbers on the line and send to a new file and look like this:

Cast Iron,Copper,Gold,Pewter,Wrought Iron,and so on. It should start a new line every 80 characters.

Thanks for the help as always!
Beaster

 
How about
Code:
{
  gsub(/[0-9]/,"")
  sub(/ *$/,"")
  if (length(a) + length + 1 > 80) {
    print a
    a = ""
  }
  a = a $0 ","
}
CaKiwi
 
I get this:

beaster@wepmas1> awk key_script new_keys2 > beaster
awk: syntax error near line 1
awk: bailing out near line 1

Using :

{
gsub(/[0-9]/,"")
sub(/ *$/,"")
if (length(a) + length + 1 > 80) {
print a
a = ""
}
a = a $0 ","
}
 
But I left the last line off when I cut and pasted.
Code:
{
  gsub(/[0-9]/,"")
  sub(/ *$/,"")
  if (length(a) + length + 1 > 80) {
    print a
    a = ""
  }
  a = a $0 ","
}
END { if (length(a)) print a}
CaKiwi
 
a variation of the theme:


#----------------------------------------------
BEGIN {
lenMAX=80;
nl=0;
}

{
$NF="";
sub(/ *$/, "");
if ((len + length($0)+1) > lenMAX) {
printf("\n%s", $0);
nl=0;
len=length($0);
}
else {
printf("%s%s", (nl==0) ? "" : ",", $0);
len=len+length($0)+1;
nl=1;
}
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
CaKiwi and vlad,

Thanks both examples worked great! Beaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top