bause@exunido.uucp (Falko Bause) (04/28/89)
Trying to go deeper into UNIX, i've a problem with the substitution of variables. I've a file named skriptyyz with line numbers in the first collumn (produced by cat -n) and a file named words which contains the words for generating a register. The following shell script should generate this register: echo REGISTER echo " " rm -f skriptyyz cat -n $1 > skriptyyz # $1 == "text" set d = `wc -l words` set e = $d[1] while ($e) set f = "`tail -$e words | head -1`" echo $f awk '/"""`$f`"""/ {print (int('$1'/48)), "", "" }' skriptyyz #?? not correct # page length is exactly 48 @ e = $e - 1 end The problem is calling awk. awk shall use the pattern in the variable f. Unfortunately f can also contain patterns like "$ber", which should be interpreted as the string (!) "$ber" and not as the variable ber. What is the right syntax for calling awk and how is variable substitution performed? And is there an easier and faster way in generating a register? Thanks Falko
ka@june.cs.washington.edu (Kenneth Almquist) (04/30/89)
bause@exunido.uucp (Falko Bause) writes: > What is the right syntax for calling awk and how is > variable substitution performed? With csh, who knows? I take it that you are trying to print out the numbers of pages containing certain words, so you want to do something like the following: if test "X$1" = X then echo Usage: $0 file exit 2 fi pagelen=48 # page length echo REGISTER echo '' while read word do awk "/$word/ {print \"$word\", int(NR / $pagelen)}" $1 done < words Run this with the shell, not csh. Kenneth Almquist
maart@cs.vu.nl (Maarten Litmaath) (05/03/89)
bause@exunido.uucp (Falko Bause) writes:
\while ($e)
\set f = "`tail -$e words | head -1`"
\echo $f
\awk '/"""`$f`"""/ {print (int('$1'/48)), "", "" }' skriptyyz #?? not correct
\ # page length is exactly 48
\@ e = $e - 1
\end
1) For EVERY line you start up 2 programs, to skip the preceding lines! :-(
Solution: use the BOURNE shell for scripts; see Kenneth Almquist's article.
2) If you're using csh's `@' feature, why not do it all the way?
@ e--
3) Can you say `indentation'?
4) What in G*d's name are you trying to do inside awk's first argument?!
Trying if inserting some more quotes will help you out?
Backquotes are used for COMMAND SUBSTITUTION; is $f a command?
(That's rhetorical.)
What you probably intended:
awk /"$f"/'{print int($1/48), "", ""}' skriptyyz
^^^^^^
Why?
Some remarks:
- if you surround $f by double quotes, the SHELL won't evaluate its
contents, but don't forget AWK knows some meta-characters too!
What about a line solely consisting of `*'? The problem is awk
expecting a regular expression, while you probably want a LITERAL
match. I haven't got a solution at hand for this one.
- The $1 is an AWK variable; the shell won't mess with it, since
it's inside single quotes.
--
"If it isn't aesthetically pleasing, |Maarten Litmaath @ VU Amsterdam:
it's probably wrong." (jim@bilpin) |maart@cs.vu.nl, mcvax!botter!maart