LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting awk to extract scripts from a file (https://www.linuxquestions.org/questions/programming-9/getting-awk-to-extract-scripts-from-a-file-35473/)

jspaceman 11-14-2002 02:45 PM

Getting awk to extract scripts from a file
 
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:
Quote:

awk 'BEGIN { RS = "*" && RS > 30; counter = 6 }
/*/ {counter = counter + 1}
{ print $0 > "awk"counter }' awkfile2
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" ???

Can someone help?

cyent 11-24-2002 03:11 PM

Moral of the story is don't.

Awk is far more difficult that it needs to be.

I _strongly_ encourage you to use Ruby, Perl or Python for such tasks.

Personally I rate Ruby a lot better than Perl or Python.

http://www.ruby-lang.org

moses 11-24-2002 04:06 PM

Code:

awk 'BEGIN { FS = "*"; counter = 6 }\
/*/ {counter = counter + 1}\
{ print $1 > "awk"counter }' awkfile2

awk isn't really that difficult, you just have to understand it, just like any
other language.

cyent 11-24-2002 04:23 PM

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. ;-)

moses 11-24-2002 05:33 PM

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).

cyent 11-24-2002 06:37 PM

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.


All times are GMT -5. The time now is 11:41 AM.