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
how to access values of awk/nawk variables outside the awk/nawk block? saniya Shell Programming and Scripting 5 05-13-2008 04:37 AM
Running command inside awk Raghuram.P Shell Programming and Scripting 1 08-09-2007 02:54 AM
if within nawk command kshelluser UNIX for Dummies Questions & Answers 1 11-30-2006 04:02 AM
nawk command not found 12yearold Shell Programming and Scripting 5 07-07-2006 11:40 AM
nawk command clarification Tux_Raju Shell Programming and Scripting 2 07-28-2005 06:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-13-2007
Registered User
 

Join Date: Dec 2007
Posts: 8
case command inside awk/nawk

well I found lot of topics about awk..about if command in awk..

but I had to implement this:

nawk -F"|" '
$47 ~ /0R0011/ { print > ("/home/user/M/MC.tmp" )}
$47 ~ /0R0012/ { print > ("/home/user/M/DuSI.tmp" )}
$47 ~ /0R0014/ { print > ("/home/user/M/FF.tmp" )}
$47 ~ /0R0018/ { print > ("/home/user/M/Cg.tmp" )}
$47 ~ /0R0010/ { print > ("/home/user/M/M1.tmp" )} ' File0


basicly I did it with if command

nawk -F "|" '
{
if ($47=="0R0011")
{
print $0 ; }
} ' File0 > MC.tmp

but for one case only ..is there some kind of case/switch command inside awk/nawk..??

thanks a lot
Reply With Quote
Forum Sponsor
  #2  
Old 12-13-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
no, there's not. but you can emulate it.
gawk 3.1.3+ does support switch/case though.

Last edited by vgersh99; 12-13-2007 at 06:44 AM.
Reply With Quote
  #3  
Old 12-13-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Edit: Just saw you use ~ and not == so the solution I posted won't work ...

You could write something like this (for consecutive numeric values you
can use split to avoid assigning explicitly the array indexes/elements)

Code:
awk 'NR == 1 {
	p = "0R00"
	h = "/home/user/M/"
	e = ".tmp"
	u[p 10] = "M1"
	u[p 11] = "MC"
	u[p 12] = "DuSI"
	u[p 14] = "FF"
	u[p 18] = "Cg"
}
$47 in u {
	print > (h u[$47] e)
}' FS="|" data
Or you can try to build your GNU Awk (>= gawk 3.1.3)
with the --enable-switch option (just found it and I've never used it)
and try the switch statement:

Quote:
switch (expression ) {
case value or regular expression :
case-body
default:
default-body
}

Last edited by radoulov; 12-13-2007 at 06:55 AM.
Reply With Quote
  #4  
Old 12-13-2007
Registered User
 

Join Date: Dec 2007
Posts: 8
that is what I thought...there is no case or switch statement inside awk/nawk...

any suggestion how can I emulate it..or how can I do it without case/switch with supported statements
Reply With Quote
  #5  
Old 12-13-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Quote:
Originally Posted by abdulaziz View Post
that is what I thought...there is no case or switch statement inside awk/nawk...

any suggestion how can I emulate it..or how can I do it without case/switch with supported statements
You already did it, no?
You want to type less?


Code:
nawk -F"|" '
$47 ~ /0R0011/ { print > ("/home/user/M/MC.tmp" )}
$47 ~ /0R0012/ { print > ("/home/user/M/DuSI.tmp" )}
$47 ~ /0R0014/ { print > ("/home/user/M/FF.tmp" )}
$47 ~ /0R0018/ { print > ("/home/user/M/Cg.tmp" )}
$47 ~ /0R0010/ { print > ("/home/user/M/M1.tmp" )} ' File0
Reply With Quote
  #6  
Old 12-13-2007
Registered User
 

Join Date: Dec 2007
Posts: 8
well yes I did it but when I look at it I just know that there is more elegant way...


but anyways thanks for answer that there is no case in awk..I thought that is must be becouse "if else" statement is supported..

i also tryed with

if () {

..}
else
{ if () {
..}
else {..}
}

but this is not working as well..
sorry for beeng boring..but I am courious..
Reply With Quote
  #7  
Old 12-13-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
Quote:
Originally Posted by abdulaziz View Post
well yes I did it but when I look at it I just know that there is more elegant way...


but anyways thanks for answer that there is no case in awk..I thought that is must be becouse "if else" statement is supported..
No, it's got nothing to do with 'if/else'. Other (most) languages support both, but not awk.
Once again, radoulov's shown one (elegant) way of emulating switch/case for your particular tasks - you might find it useful to adopt.
Quote:
Originally Posted by abdulaziz
i also tryed with

if () {

..}
else
{ if () {
..}
else {..}
}

but this is not working as well..
sorry for beeng boring..but I am courious..
not sure if it's the entire script, but it seems like you have an unbalanced '}'. What exactly 'not working'?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:33 AM.


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

Content Relevant URLs by vBSEO 3.2.0