ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a text file with letters that are all CAP and have no space in between them. I wanted to write a gawk script that pulls out only the letters that are in the "string" and prints them to a new file. After much strife with the asort() function, which produced unpredictable results (gawk version 3.1.3, supposedly this is fixed in version 3.1.7), I wrote a script that gives me reliable and consistent results, so I wanted to share.
Here's the pseudo-code:
Code:
awk 'BEGIN
{
FS = "";
RS = "\n"
};
{
for(i=65;i<=90;i++)
{
letters[i]=sprintf("%c",i)
};
for(j=65;j<=90;j++)
{
match_letters[j]=0
};
for(k=1;k<=NF;k++)
{
for(m=65;m<=90;m++)
{
if($k ~ letters[m])
{
match_letters[m]++
}
}
};
for(n=65;n<=90;n++)
{
if(match_letters[n] > 0)
{
printf"%c",n
}
};
printf"\n"
}' input_file > output_file
The idea is that there is a string with no spaces and you only want to know what letters are in the string and then print them in order. The major problem with awk is that you cannot evaluate characters as an integer.
Last edited by staticd; 02-09-2010 at 10:51 PM.
Reason: correct code format
Staticd: Your psuedo-code would be more readable if you used indentation.
Indeed, I used the appropriate indentation but it must have been lost in translation. I tried several times and the indentation tags never held. I think I know what I did wrong now. The indentation tags have to go before and after each line that must be indented?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.