Operating System - HP-UX
1833777 Members
2153 Online
110063 Solutions
New Discussion

Re: Best scripting instrument ?

 
SOLVED
Go to solution
enrico.nic
Regular Advisor

Best scripting instrument ?

Hi all,
I need to do something simple: from a series of data text files with columns divided by one or more blanks, I need first to count the number of columns (fields), and the print out on stdout the first 6 columns, then the last 31, and then the rest of the columns.
I am obviously in a hurry, and I am facing some problems with a short C program that uses strtok, strcat etc.
Has anyone a better idea than using C ?

Thank you

Enrico
3 REPLIES 3
Mark Grant
Honored Contributor

Re: Best scripting instrument ?

Enrico,

Everyone will have a different view on this but "perl" is going to be heaven for this kind of thing. It is remarkably good and string and data handling. I hardly ever use 'C' these days, it's just so much better to do the thing with perl.
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Best scripting instrument ?

If the length of the input records does not exceed 3000 characters then awk is probably your quickest to learn method.

Try this, create a file, my.awk:


{
n = split($0,arry)
i = 1; # awk arrays are 1-based
while (i <= 6)
{
printf("%s ",arry[i])
++i
}
i = n - 31;
while (i <= n)
{
printf("%s ",arry[i]);
++i;
}
i = 7;
while (i < (n - 31))
{
printf("%s ",arry[i]);
++i;
}
printf("\n");
}


now awk -f my.awk < infile

If the input lines exceed 3,000 characters then get the Gnu version of awk. My real favorite -- although the learning curve is steeper but worth it -- is Perl. Well. written Perl will execute almost as faat as the C equivalent and code in a fraction of the time --- and port unchanged to Windows.
If it ain't broke, I can fix that.
enrico.nic
Regular Advisor

Re: Best scripting instrument ?

Thank you all for the prompt reply. The awk solution worked for me.
I should begin to study Perl in the near future ..

Thank you again

Enrico