Computing.Net > Forums > Linux > variable in AWK command in script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

variable in AWK command in script

Reply to Message Icon

Name: Bobby
Date: November 24, 2003 at 08:47:34 Pacific
OS: linux, bash shell
CPU/Ram: p4 256DDR
Comment:

Hi,

I just started to learn awk/sed/grep and we need to write a shell script which will randomly choose 2 numbers and from those numbers pick words out of a dictionary file. There is more but thats where i am having problems so far.

This is what i have got for it:

#!/bin/bash
#Created by me
#November 17, 2003

numb_ppl=$(grep -c '.*' /usr/share/dict/words) #finds number of ppl in file
num1=$(perl randm.pl.txt $numb_ppl) #uses perl random script to choose
number between 1 and numb_ppl
num2=$(perl randm.pl.txt $numb_ppl)

grep -n '.*' /usr/share/dict/words > holdgrep #redirects output to a new file

#i have checked here and everything works fine

awk -F: '/$num1/{print $2}' holdgrep
--------END OF S-----------

I never get an output for field2 in the holdgrep file. I am assuming my syntax for a variable in an awk statement is off somehow but i am not sure. Does anyone have any ideas?



Sponsored Link
Ads by Google

Response Number 1
Name: gmoney
Date: November 24, 2003 at 21:06:50 Pacific
Reply:

Is your instructor ok with you posting homework questions?
Anyway, you're on the right track so I'll give you a hint; look in awk (or in my case, gawk) man pages for the -v or --assign option.
Good luck.


0

Response Number 2
Name: Bobby
Date: November 25, 2003 at 17:27:55 Pacific
Reply:

Hmm, well the -v that i read didnt make sense till someone on another board explained it to me (odd, you both thought the same thing), but i figured out the problem in my code. I needed to add single quotes around $num1

But my teacher didnt like the way i was coding. Too complicated, sheesh. So he showed me how he wanted it. Same result, different code.

How's that for teaching creative coding :S

Thanks though, i did learn some about -v option


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


How Get Mac Address emu10k1/AC97 optimal mixe...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: variable in AWK command in script

passing variables in UNIX www.computing.net/answers/linux/passing-variables-in-unix/26609.html

Looping through records cutting www.computing.net/answers/linux/looping-through-records-cutting/26846.html

variable in kickstart file www.computing.net/answers/linux/variable-in-kickstart-file/29378.html