Jeff
Jeff

Reputation: 263

Self-contained awk script: Saving to file, calling file

For a lab, I wrote a shell script that used awk to do some stuff. Rereading the lab's directions, it seems that I was supposed to write a self-contained awk script. I'm working on translating my bash script into awk, and I'm having a problem right now:

I want to save the output of an awk command to a new file, and then I want to use that output as input for another awk command.

In my bash script, I have this:

awk '/Blocked SPAM/' maillog > spamlog
cat spamlog | awk '{print $0}' RS=' '

It takes all the lines from maillog that contain the string "Blocked SPAM" and saves this to a new file titled spamlog. Then it opens spamlog and replaces every space character ' ' with a new line.

For my awk script, maillog is the file that is passed to the script from shell. My attempt at writing analogous code:

/Blocked SPAM/ > spamlog`
-f spamlog {print $0} RS=' '

I don't really know what I'm doing with my awk script since I'm having trouble finding useful resources for self-contained awk scripts.

Upvotes: 0

Views: 1007

Answers (1)

William Pursell
William Pursell

Reputation: 212288

awk '/Blocked SPAM/{ print > "spamlog"; gsub( " ","\n"); print }' maillog

Personally, I prefer to invoke that directly from a shell script, but you can easily make it an awk script by writing:

#!/usr/bin/awk -f

/Blocked SPAM/{ print > "spamlog"; gsub( " ","\n"); print }

Invoke that script with 'maillog' as an argument.

Upvotes: 2

Related Questions