Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk problem, unix (rocks) newbie 2

Status
Not open for further replies.

ivanmcp

Technical User
Apr 25, 2003
14
GB
Hi everyone,

I am working on a small language translation program, and am stuck trying to use awk.
I have so far created three files:

1. main menu file
2. lookup file with awk, which is called from main menu file
3. language file containing english, french and serbian words, like:

sea mer more
water eau voda

I want user to enter english word, and french equivalent should appear on the screen. I want to store user input - like 'car' into variable, which is then used as search term. Once line containing 'car' is found, I want to display word in second column ($2), which is french equivalent.

This is my code:

echo "Enter the word in english:"
read string
echo "Same word in french is:"
awk $1=$string '{print $2}' lang_words.txt
echo
echo "press any key to continue"
read key

I get ^ parse error when trying to run this, and have tried all combinations on this theme, but to no avail. Can somebody help me get over this, as I have no other ideas.

Ivan K. Grant
 
script x:

echo "Enter the word in english:"
read string
echo "Same word in french is:"
grep $string lang_words.txt | awk '{print $2}'
echo
echo "press any key to continue"
read key

lang_words.txt looks like:
the de

screen shows:
> x
+ x
Enter the word in english:
the
Same word in french is:
de

press any key to continue

>
 
A slight alteration to cruels' script...

use grep -w $string to search for the "word". By just searching for a string of text, user could get much unwanted output. :-0

Cheers
M
 
Closer to script by ivanmcp ...

#!/bin/sh

echo -n "Enter the word in english: "
read myWord
echo -n "Same word in french is: "
awk -v word=$myWord '{
if ($1 == word) {print $2}
}' lang_words.txt
echo
echo -n "press any key to continue"
read key

 
Hey guys, thank you very much. I can not believe what a quick response I received. I am going to try your code now.
 
Just tried pavNell code, and this is the response:

Same word in french is: awk: cmd. line:2:    if ($1 == word) {print $2}
awk: cmd. line:2: ^ invalid char ' ' in expression
 
Tried cruel's code and this is the output:

Enter the word in english:
car
Same word in french is:
voiture
echarpe

echarpe is displayed, as there is another word that contains car - scarf (echarpe) :)
 
Ok,

This one seem to work fine, and is based on pavNell's example:

echo "Enter the word in english:"
read string
echo "Same word in french is:"
awk ' { if ($1 == word)
print $2 }' word="$string" lang_words.txt
echo
echo "press any key to continue"
read key

Thanx guys for all your help. I've been using Unix for 1 week and find it very exciting. Hope to learn more about scripting.
 
What is a word?
Is "cat's" a word?
Is "cat?" a word?
Is "cat," a word?
Is "cat/dog" a word?

Sorry........ it's been a long day

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hehe, I just need a basic word, like dog OR cat. If user makes a mistake or enters something different from the words in a file, god help me. This will suffice, as I am trying to see how awk command works at its most basic level.
I wouldn't know where to start with user's input error checking or whatever one would call such a feature.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top