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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk syntax using piping and fgrep 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a 2 part question, still related to my other posts:

PART I:

i have a file database of variables in the form

var1:var2:var3:::

ex.

%celebrities.csh
michael jordan:basketball player:man::
britney spears:singer:woman::
madonna:singer:woman:::

---------------------------------------
looking at this code snippet:

echo -n "\nEnter a celebrity: "
set celebrity = $<
echo -n &quot;Enter Job: &quot;
set job = $<

if (`fgrep -c &quot;${celebrity}&quot; celebrity.csh`) then

(`fgrep -w &quot;${celebrity}&quot; celebrity.csh`) | fgrep &quot;:man:&quot; | awk -F: -f celebrity.csh '{printf &quot;$3=%s&quot;, woman}'

else
echo -n &quot;***Error*** No Celebrity by tht Name&quot;
goto MainMenu
endif
-------------------------------------------------------

###i am trying to first check if the celebrity entered by the user is in my database (this works)
then
pipe the result of that line to another search that checks to see if that celebrity is a man (this also works)
then if true (is a man) pipe that into an awk command that would change the 3rd varible value in that database file (in this case &quot;man&quot;) to &quot;woman&quot;
(this part is not working for me)

...so ex:
if user enter michael jordan, it performs a search to see if he is in the database file called celebrities.csh,
then checks to see if he is a man,
then should change the man variable to woman so if i looked at the celebrities.csh file it should look like this:

michael jordan:athlete:woman:: ##man status changed
britney spears:singer:woman::
madonna:singer:woman:::

i am not familiar with the awk utility and would like assistance in doing this please...


Also PART II:

using same database file, after having the user enter a celebrity, and do the search to see if they are in the database, how would i code i guess another awk command to remove that line that contains the entered celebrity from the database file??

---------------------------------------
looking at this code snippet:

echo -n &quot;\nEnter a celebrity: &quot;
set celebrity = $<
echo -n &quot;Enter Job: &quot;
set job = $<

if (`fgrep -c &quot;${celebrity}&quot; celebrity.csh`) then

##remove that matching line

else
echo -n &quot;***Error*** No Celebrity by tht Name&quot;
goto MainMenu
endif
-------------------------------------------------------



any help would be appreciated!
thanks,
jada
 
Hi,
you are giving AWK conflicting options.

-f is supposed to be a file of AWK command not the file you want to awk.

awk -f a.awk myfile

where a.awk contains

{printf (&quot;%s\n&quot;,$3);}

or

awk '{printf (&quot;%s\n&quot;,$3);}' myfile



but not both.
 
ok, thanks for the tips, but i still dont know how to write the syntax so that it would overwrite &quot;man&quot; in the 3rd variable to &quot;woman&quot;

or how to delete that line completely (refering to part II)

thanks
 
i was reviewing the sed utility...
would the code better be written using sed instead of awk,
is this possible?

thanks
 
ok i dont think sed is wht i need since it only outputs the results to standardoutput instead of writing it to the file...
 
Sed's output can be redirected to a new file, than the original can be overwritten, if required.
e.g
cat oldfile|sed 's/man/woman/g' > newfile
mv newfile oldfile
HTH. Dickie Bird
db@dickiebird.freeserve.co.uk
 
Awk can replace the two piped fgrep and the last awk. Try the following inside the then part of your if (all on one line):
Code:
awk 'BEGIN {FS=OFS=&quot;:&quot;} /^'&quot;${celebrity}&quot;':.*:man:/ { $3=&quot;woman&quot;; } { print; }' < celebrities.csh > celebrities.csh.new
 
Hi,
but doesn't this change Jordon to a Woman? just wondering? I mean what is the purpose of the script?

---
 
hey ,

just wanted to say thanks guys for all the help,
especially dchoulette, his code is what exactly i was looking for.

as for tdatgod, tht part about the woman/man, i was just using that as an example to learn how to over-write a field..lol...thanks again!

jada
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top