![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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
with the --enable-switch option (just found it and I've never used it) and try the switch statement: Quote:
Last edited by radoulov; 12-13-2007 at 06:55 AM. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
Quote:
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
|
|
#6
|
|||
|
|||
|
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.. |
|
#7
|
||||
|
||||
|
Quote:
Once again, radoulov's shown one (elegant) way of emulating switch/case for your particular tasks - you might find it useful to adopt. Quote:
|
||||
| Google The UNIX and Linux Forums |