LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2002, 02:45 PM   #1
jspaceman
Member
 
Registered: Dec 2000
Location: Toronto, Canaduhhhh
Distribution: Slackware -current, OpenBSD 3.8-stable
Posts: 178

Rep: Reputation: 30
Question 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?
 
Old 11-24-2002, 03:11 PM   #2
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
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
 
Old 11-24-2002, 04:06 PM   #3
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
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.

Last edited by moses; 11-24-2002 at 04:07 PM.
 
Old 11-24-2002, 04:23 PM   #4
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
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. ;-)
 
Old 11-24-2002, 05:33 PM   #5
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
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).
 
Old 11-24-2002, 06:37 PM   #6
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
how to extract the name of the file bahadur Programming 9 03-22-2005 06:48 PM
Help needed in writing Awk Scripts.. TheDarktrooper Red Hat 1 05-04-2004 06:49 PM
Extract file Electboy Linux - General 1 07-21-2003 05:51 PM
How do you extract a file? Neb Linux - Newbie 9 05-14-2003 03:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration