AWK Question

TheSpook

Limp Gawd
Joined
Feb 12, 2004
Messages
379
Hello hello -- never used AWK before, but I hear it's a mighty fine search and replace tool.

Here's my issue: I would like to replace every line that contains "prevBoard[x][y][z]" with "MYINDEX3D(prevBoard, n, n2, x, y, z)", where x, y, and z represent variable text of max length 3.

I have written the following AWK script:

Code:
awk '\
{
        sub(/prevBoard[x][y][z]/, "MYINDEX3D(prevBoard, n, n2, x, y, z)");
        print
}'

However, I want to tell AWK that x, y, and z are variables. I don't want it thinking that x, y, and z are literally written into the code. How would I go about doing that?

Thanks in advance.


Edit: Also, how to tell AWK to run this on a certain file? Right now this is saved as "f_and_r.sh" -- would I do something like ./f_and_r.sh filename.txt ?

Edit2: Looks like I can do: awk -f f_and_r.sh filename.txt. Guess I'll just pipe that output into a new text file or something.
 
You'd probably be just as well off, or better, using sed. Sed's better for straight substitutions, AWK's more for things that require logic & a concept of state and/or 'fields' on the line.

With that said, here's a sed line for doing it:
Code:
sed "{s/prevBoard\[\(.*\)\]\[\(.*\)\]\[\(.*\)\]/MYINDEX3D(prevBoard, n, n2, \1, \2, \3)/g }" infile

Like all regular expressions, this is ugly as sin and has too many . If you wanna get into it yourself, the key word is "back references".

.* is just some string of characters
\(.*\) is a string of characters that are "grouped"
\[\(.*\)\] is that grouped string of characters surrounded by brackets ([s in regexes have special meanings so they need to be escaped if you just want to have a character on the line)

once you have a grouped expression, you can refer to it later with \1 for the first group, \2 for the 2nd and so on.
 
Thank you much -- it was the /1, /2, /3 whatnot I didn't get.

In general, sed is used for find-and-replace in files, whereas awk is used for just finding and returning?

If that's the case, what's the point of awk?
 
TheSpook said:
If that's the case, what's the point of awk?

It's just there to give you more choices. Arguably, both sed and awk are deprecated in favor of Perl (which was created to replace both of them).
 
TheSpook said:
Thank you much -- it was the /1, /2, /3 whatnot I didn't get.

In general, sed is used for find-and-replace in files, whereas awk is used for just finding and returning?

If that's the case, what's the point of awk?


Awk has the ability to maintain state, variables, have program flow and whatnot. It can handle things that have more complex logic than what sed does. As a rule-of-thumb, I'd use sed when each line works regardless of those around it and awk when what I need to do with one line depends on what's on the next line.

Sed sees a file as a bunch of lines of text. Awk sees a file as an ordered list of lines and those lines are 'fields' of text.

Let's say you have a tab-separated file, and, depending on the value of the first column, you want to sum up the 3rd or the 2nd+4th columns. Awk is -perfect- for this.
 
Back
Top