The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Trying to implement case ssunda6 Shell Programming and Scripting 2 03-21-2008 02:55 AM
Script needed to select and delete lower case and mixed case records abhilash mn Shell Programming and Scripting 1 03-17-2008 04:00 AM
To CASE or not to CASE, this is my question! olimiles Shell Programming and Scripting 1 08-19-2006 01:54 AM
Ignore case sensitive in Case Switch annelisa Shell Programming and Scripting 1 07-13-2006 01:36 AM
lower case to upper case string conversion in shell script dchalavadi UNIX for Dummies Questions & Answers 3 05-28-2002 09:07 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Feb 2008
Posts: 7
Trying to implement case

Hi All,

My requirement is if the record is starting with 0, then do some processing.
if starting with 1, some processing
else (not with 0 or 1 ) then do some other processing.

i tried the following
Code:
case "$test" in
/^0/) echo "starting with zero ;;
/^1/) echo " with one" ;;
*) echo "otherwise"
esac
Though the record is starting with zero, it is printing "otherwise" everytime.

Tried implementing throuh awk also.

Code:
echo $test | awk '
/^0/ { print "starting with zero" }
/^1/ { print "with 1" }
'
But I am unable to figure out how to tell awk to do something whenever both the patterns are not matched.
Please provide inputs.
Regards.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-21-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,667
Is this what you're looking for?

Code:
echo $test | awk '{
if (/^0/){
  print "YES"}
else {
  print "NO"}
}'
Regards
Reply With Quote
  #3 (permalink)  
Old 03-21-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,667
After reading the question agian an additional answer to find out whenever both patterns are not matched:

Code:
echo $test | awk '{
if (!/^0/&&!/^1/){
  print "Both not matches"}
else {
  print "Starting with 0 or with 1"}
}'
Regards
Reply With Quote
  #4 (permalink)  
Old 03-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,111
The shell does not use slashes around case patterns. What you are looking for is

Code:
case "$test" in
  0*) echo "starting with zero ;;
  1*) echo " with one" ;;
  *) echo "otherwise";;  # note closing double semicolon here too
esac
awk simply falls through even if there is a match, so you can speculate what this does:

Code:
echo $test | awk '
/^0/ { print "starting with zero" }
/^1/ { print "with 1" }
/^[01]/ { print "neither" }
{ print "any of the above" }'

Last edited by era; 03-23-2008 at 02:45 AM. Reason: Actually the double quotes in 'case "$test" in '... are not required, but I left them in anyway
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0