- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk, piping, replacing a string
Operating System - HP-UX
1753564
Members
5513
Online
108796
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2002 08:56 PM
тАО07-24-2002 08:56 PM
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 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2002 10:52 PM
тАО07-24-2002 10:52 PM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2002 11:23 PM
тАО07-24-2002 11:23 PM
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
-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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2002 05:21 AM
тАО07-25-2002 05:21 AM
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
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
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP