Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
cancel
Showing results for 
Search instead for 
Did you mean: 

awk, piping, replacing a string

tskanes
Occasional Visitor

awk, piping, replacing a string


I have a 2 part question:

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 "Enter Job: "
set job = $<

if (`fgrep -c "${celebrity}" celebrity.csh`) then

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

else
echo -n "***Error*** No Celebrity by tht Name"
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 "man") to "woman"
(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 "\nEnter a celebrity: "
set celebrity = $<
echo -n "Enter Job: "
set job = $<

if (`fgrep -c "${celebrity}" celebrity.csh`) then

##remove that matching line

else
echo -n "***Error*** No Celebrity by tht Name"
goto MainMenu
endif
-------------------------------------------------------



any help would be appreciated!
thanks,
jada
3 REPLIES
Brian Crabtree
Honored Contributor

Re: awk, piping, replacing a string

Part I.

I dont know of a way to do this with awk, but someone on here may know how. You could do what you are talking about with 'sed' however. It would need to be something like:

sed -e 's/${celebrity}:${job}:man/${celebrity}:${job}:woman/'

Part II.

I would do something like:

cat celebrity.csh | grep -v $celebrity > celebrity2.csh
mv celebrity2.csh celebrity.csh

Hope this helps,

Brian
Ceesjan van Hattum
Esteemed Contributor

Re: awk, piping, replacing a string

Hi, what about this:

-v option to import variable
FS is set Field Seperator

# change gender to woman
INPUTFILE=celebrities.csh
NAME="michael jordan" # set by reading input yourself

awk -v name=$NAME 'BEGIN{FS=":"}{
if ($1=name) { print "$1:$2:woman::" }
else { print $0 }
}' $INPUTFILE

=========

# switch gender if found
awk -v name=$Name 'BEGIN{FS=":"}{
if ($1=name) {
if ($3="man") { print "$1:$2:woman::" }
else { print "$1:$2:man::" }
else { print $0 }
}' $INPUTFILE

=========

# delete found celeb
awk -v name=$Name 'BEGIN{FS=":"}{
if ($1!=name) { print $0 }
}' $INPUTFILE

Regards,
Ceesjan
tskanes
Occasional Visitor

Re: awk, piping, replacing a string

thanks guys

because of yall i was able to figure alot of things out and get my program on it's way to being finished....

you guys are great

;o]
jada