Sponsored Content
Full Discussion: case command inside awk/nawk
Top Forums Shell Programming and Scripting case command inside awk/nawk Post 302150985 by radoulov on Thursday 13th of December 2007 08:32:21 AM
Old 12-13-2007
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 09:55 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

2. Shell Programming and Scripting

How to specify 'not case sensitive' in regex (nawk, sed, patern expencions)?

Is it possible to make the search in regular exprecion or in matching parts of sed, nawk and others to IGNORE the case of the search string? I mean, like if used 'grep' with -i option: > grep -i "abc" file I would like to be able to do the same, say, by nawk: > nawk '/abc/ {print $0}'... (4 Replies)
Discussion started by: alex_5161
4 Replies

3. Shell Programming and Scripting

using cp command inside nawk

Hello I have facing issue while using cp command inside nawk block. #!/bin/ksh my_name=$1 nawk -v my_name1=$my_name 'BEGIN { n = split(my_name1,names,":"); for (i=1;i<=n;i++) { print names; cpcmd = "cp " /tmp/test.txt" " ./sample system(cpcmd) } exit }' exit 0 i'am getting... (1 Reply)
Discussion started by: piscean_n
1 Replies

4. Shell Programming and Scripting

How to use same variable value inside as well as outside of the awk command?

Hi Jim, The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk. My problem is i want to maintain one property file in which i am declaring variable value into that... (12 Replies)
Discussion started by: Ganesh Khandare
12 Replies

5. Shell Programming and Scripting

problem in nawk : case insensitive pattern matching

HI, My file contains data something like 034500,5,B5004946544EB185,DEFAULT,0 Now i want to do a pettern match for DEFAULT and remove that particular line from file and transfer the rest contents to temp file.But my req is i want to do case insensitive matching ie DEFAULT / default. I... (4 Replies)
Discussion started by: centurion_13
4 Replies

6. Shell Programming and Scripting

Reading input record from inside nawk

Hi friends, I have small query with reg to awk search pattern.. below is my sample file and code which i tried.. $ cat file.txt xxx,yyyyy,messageID,sha xxxx,errorcode,messageID,name in the above sample file - let assume I know the errorcode(2nd record) using which I want to... (2 Replies)
Discussion started by: Shahul
2 Replies

7. Shell Programming and Scripting

Nested case inside awk

please let me know if the below code could be written efficiently inside single awk case "$INP" in ksh) cat catalog | awk 'BEGIN {FS=",";} { print $2 } END {}' ;; pset) cat catalog | awk 'BEGIN {FS=",";} { print $3 } END {}' ;; dml) cat catalog | awk 'BEGIN {FS=",";} {... (2 Replies)
Discussion started by: cvsanthosh
2 Replies

8. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

Substitute variable inside nawk

Hi, I need to set "prd" in the below command to a unix variable nawk '/^#/ {next} FNR==NR {prd;next} !($0 in prd)' So, this is what i did fname=prd // unix shell variable nawk -v fname=$fname '/^#/ {next} FNR==NR {fname;next} !($0 in fname)'But the value of fname i.e "prd" is not... (8 Replies)
Discussion started by: mohtashims
8 Replies

10. UNIX for Beginners Questions & Answers

How to avoid arguments inside Nawk command?

Hi, Here is my command print $2 was meant to select the second column however, it is getting substituted with the second argument that was passed to the script. Can you please tell me how can I resolve this ? (6 Replies)
Discussion started by: mohtashims
6 Replies
switch(3tcl)						       Tcl Built-In Commands						      switch(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
switch - Evaluate one of several scripts, depending on a given value SYNOPSIS
switch ?options? string pattern body ?pattern body ...? switch ?options? string {pattern body ?pattern body ...?} _________________________________________________________________ DESCRIPTION
The switch command matches its string argument against each of the pattern arguments in order. As soon as it finds a pattern that matches string it evaluates the following body argument by passing it recursively to the Tcl interpreter and returns the result of that evaluation. If the last pattern argument is default then it matches anything. If no pattern argument matches string and no default is given, then the switch command returns an empty string. If the initial arguments to switch start with - then they are treated as options unless there are exactly two arguments to switch (in which | case the first must the string and the second must be the pattern/body list). The following options are currently supported: -exact Use exact matching when comparing string to a pattern. This is the default. -glob When matching string to the patterns, use glob-style matching (i.e. the same as implemented by the string match command). -regexp When matching string to the patterns, use regular expression matching (as described in the re_syntax reference page). | -nocase | Causes comparisons to be handled in a case-insensitive manner. | -matchvar varName | This option (only legal when -regexp is also specified) specifies the name of a variable into which the list of matches found by | the regular expression engine will be written. The first element of the list written will be the overall substring of the input | string (i.e. the string argument to switch) matched, the second element of the list will be the substring matched by the first | capturing parenthesis in the regular expression that matched, and so on. When a default branch is taken, the variable will have | the empty list written to it. This option may be specified at the same time as the -indexvar option. | -indexvar varName | This option (only legal when -regexp is also specified) specifies the name of a variable into which the list of indices referring | to matching substrings found by the regular expression engine will be written. The first element of the list written will be a | two-element list specifying the index of the start and index of the first character after the end of the overall substring of the | input string (i.e. the string argument to switch) matched, in a similar way to the -indices option to the regexp can obtain. | Similarly, the second element of the list refers to the first capturing parenthesis in the regular expression that matched, and | so on. When a default branch is taken, the variable will have the empty list written to it. This option may be specified at the | same time as the -matchvar option. -- Marks the end of options. The argument following this one will be treated as string even if it starts with a -. This is not | required when the matching patterns and bodies are grouped together in a single argument. Two syntaxes are provided for the pattern and body arguments. The first uses a separate argument for each of the patterns and commands; this form is convenient if substitutions are desired on some of the patterns or commands. The second form places all of the patterns and commands together into a single argument; the argument must have proper list structure, with the elements of the list being the patterns and commands. The second form makes it easy to construct multi-line switch commands, since the braces around the whole list make it unnec- essary to include a backslash at the end of each line. Since the pattern arguments are in braces in the second form, no command or vari- able substitutions are performed on them; this makes the behavior of the second form different than the first form in some cases. If a body is specified as "-" it means that the body for the next pattern should also be used as the body for this pattern (if the next pattern also has a body of "-" then the body after that is used, and so on). This feature makes it possible to share a single body among several patterns. Beware of how you place comments in switch commands. Comments should only be placed inside the execution body of one of the patterns, and not intermingled with the patterns. EXAMPLES
The switch command can match against variables and not just literals, as shown here (the result is 2): set foo "abc" switch abc a - b {expr {1}} $foo {expr {2}} default {expr {3}} Using glob matching and the fall-through body is an alternative to writing regular expressions with alternations, as can be seen here (this returns 1): switch -glob aaab { a*b - b {expr {1}} a* {expr {2}} default {expr {3}} } Whenever nothing matches, the default clause (which must be last) is taken. This example has a result of 3: switch xyz { a - b { # Correct Comment Placement expr {1} } c { expr {2} } default { expr {3} } } When matching against regular expressions, information about what exactly matched is easily obtained using the -matchvar option: | switch -regexp -matchvar foo -- $bar { | a(b*)c { | puts "Found [string length [lindex $foo 1]] 'b's" | } | d(e*)f(g*)h { | puts "Found [string length [lindex $foo 1]] 'e's and | [string length [lindex $foo 2]] 'g's" | } | } | SEE ALSO
for(3tcl), if(3tcl), regexp(3tcl) KEYWORDS
switch, match, regular expression Tcl 8.5 switch(3tcl)
All times are GMT -4. The time now is 04:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy