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 am trying to write an awk extraction script that will extract scripts from a file called awkfile2. There are 4 scripts in awkfile2, written like this:
Quote:
******************************************************
{ if ( disksize < $5 )
{
disksize = $5
computer = $0
}
}
*****************************************************
/386/ { for (field = 1; field <= NF; field += 2)
printf("%s\t", $field)
print ""
}
*****************************************************
/286/ { field = 1
while ( field < = NF)
{
printf ("%s\t",$field )
field += 2
}
print ""
}
*****************************************************
BEGIN { field =1 }
$1 == "386" { do {
printf("%s\t", $field)
field +=2
} while( field <= NF )
}
******************************************************
I want awk to extract each of the four above scripts and write each of them to their own separate file, starting at awk6 all the way up to awk9.
So far this is what I have for my awk extraction script:
This extracts the files, sort of, but it doesn't put each of them into their own separate file (ie. awk6, awk7, awk8, awk9). Instead it extracts part of one script, puts it into a file, and then extracts part the next script, puts it into a file, etc. Perhaps something is wrong with the line "RS = "*" && RS > 30" ???
No, it is not difficult to understand and use. I have used it a lot.
It is just difficult to do many tasks.
If your job is to iterate through a file matching certain patterns and perfoming an action on the line that matched that pattern. Awk is good. Many tasks fit that description.
Many more tasks don't.
Odds on if you are struggling to do something in awk, it will be trivial in Python or Perl or Ruby.
Hence a generic answer to any question on "How do I do XXX in awk?" is "Don't. Do it in Perl or Python or Ruby." Because if they need to ask how on a forum such as this, it is more than likely a problem that is trivial in one of those languages.
After all, that was one of the very design goals of Perl.
The design goal of Ruby was to be better than Perl. ;-)
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
I agree that there are tasks that are more easily done with PERL or Ruby,
but I don't think they can completely obviate the usefulness of awk, sed,
or any of the other nice, small, quick, (obscure, obfuscated, complex) tools
that have been around for N years (and with good reason).
Yip. As I said, many tasks fit the poster child description for an awk problem. So use awk on them. But as soon as you start to struggle, stop and reach for Ruby.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.