- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Awk script help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
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
Discussion Boards
Community
Resources
Forums
Blogs
- 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-19-2005 10:56 PM
тАО07-19-2005 10:56 PM
{
if ($2="ABC")
{printf $1}
if ($2="BCD")
{printf $3}
} etc.....
This code is executing every print statement nomatter what column 2 has in it.
What am i missing?
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2005 11:05 PM
тАО07-19-2005 11:05 PM
Solutionif ($2 == "ABC") { print $1 }
else if ($2 == "BCD") { print $3 }
etc. etc.
NOTE: I highly recomend you use 'else if'.. saves a fair bit of processing, especially if you have a number of possibilities, unless you throw a 'next' in there somewhere.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2005 11:10 PM
тАО07-19-2005 11:10 PM
Re: Awk script help
it should be == not =.
Have you tried without if ?
$2=="ABC" {printf $1}
$2=="BCD" {printf $3}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2005 11:41 PM
тАО07-19-2005 11:41 PM
Re: Awk script help
awk ' $2 ~ /ABC/ {print $1} $2 ~ /BCD/ {print $3}' filename
I hope it helps.
Juan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2005 12:59 AM
тАО08-01-2005 12:59 AM
Re: Awk script help
As you have discovered, awk syntax is a pig.
In scripts, I get around it by using functions. This means more typing, but is much easier to code (and read)
e.g.
Say the line you want to extract fields from is in a variable called "myline"...
#!/usr/bin/ksh
myfunc()
{
if [[ $2 = "ABC" ]] ; then
echo $1
elif [[ $2 = "DEF" ]] ; then
echo $3
# etc...
fi
}
#Main code
myline="ABC DEF GHI JKL MNO PQR STU VWX YZ"
myfunc $myline
The above will echo GHI to the screen.