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

no, there's not. but you can emulate it.
gawk 3.1.3+ does support switch/case though.

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)

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:

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? :slight_smile:

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

well yes I did it but when I look at it I just know that there is more elegant way...:slight_smile:

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..:slight_smile:

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.

not sure if it's the entire script, but it seems like you have an unbalanced '}'. What exactly 'not working'?

I ran into the same issue, and I found about this switch >--enable-switch< but does not seem to work :confused:

nawk --enable-switch -f program.awk some_file

I'm using Sun Solaris 5.9 and i'm not sure what version of nawk or awk I have

Did anyone figure out how to enable the use of switch/case consturcts in awk/nawk?

Thanks,

if you look through the earlier posts in this thread, you'll see that '--enable-switch' is supported by gawk 3.1.3++.

if your version doesn't have it, just use if/else. switch and if/else does the same thing.