Nested case inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested case inside awk
# 1  
Old 04-27-2012
Nested case inside awk

please let me know if the below code could be written efficiently inside single awk
Code:
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=",";}     { print $4 }  END {}' 
;;
esac


Last edited by Franklin52; 04-27-2012 at 05:39 AM.. Reason: Please use code tags for code and data samples
# 2  
Old 04-27-2012
How about this ?
Code:
awk -F"," -vinput="$INP" '{if(input ~ /ksh/){print $2}
                        if(input ~ /pset/){ print $3 }
                        if(input ~ /dml/) { print $4 }
                        }' filename

# 3  
Old 04-27-2012
Code:
# awk -vinp=$INP -vi=2 'inp=="ksh"{print $i};inp=="pset"{print $(i+1)};inp=="dml"{print $(i+2)}'  FS=","

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Case inside While read File

Hi Experts, Need your guidance for case statement. I tried many way but no success yet.Now my existing code is doing something like below. Each Line of the input file contains one test case.#!/bin/bash FILE=$1 while read LINE; do do COMMAND done < $FILE Now I want to modify the code... (6 Replies)
Discussion started by: pradyumnajpn10
6 Replies

2. Shell Programming and Scripting

Nested case: rsync command not found

Hello! Below my first bash script. As you can see i build 2 nested cases. The second one (sync databases) is working fine. Bu the first one (sync datadirs) is not working. It says: rsync: command not found. However when i move the rsync command to the top of the script its working. So i suppose... (2 Replies)
Discussion started by: hyperconnected
2 Replies

3. Shell Programming and Scripting

Error with nested if within an sqlplus task inside

Hi ALL, I am receving a "strange" error using a nested if within an sql operation inside: ./dom.ksh: syntax error at line 80 : `then' unmatched This is all my script code: in bold the step receiving the error. Any help would really aprrecieted ......! **** I have tried all the... (2 Replies)
Discussion started by: AndreaCecco
2 Replies

4. Shell Programming and Scripting

Nested case

Hi there, I have nested case in my script. I am asking user, want to continue? if user press y/Y then my inner case should continue, rather than that my code start from beginning. I would like to continue my inner case until user press n or N. Is any one tell me how can I do? Thanking You,... (2 Replies)
Discussion started by: kasparov
2 Replies

5. Red Hat

Repacking a file inside a java archive with nested .jars

Hello, I need to repack a file inside several java archives (nested .jar files) with or without overwrite. I am using a manual approach with mc, but it's painfully slow progress. For example I want to refresh a file deep inside a java archive (with nested .jar files): I have a java... (0 Replies)
Discussion started by: Laurentiu
0 Replies

6. Shell Programming and Scripting

If-statement nested in case

I'm trying to write case statements with 'if statements' embedded inside of them. I'm using the korn shell but it's not functioning. If I want to see if a string exists in a file and then perform an action, what would be the best way to do this? For file "asg51fin" to delete a line if a... (1 Reply)
Discussion started by: dazeman27
1 Replies

7. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

8. Shell Programming and Scripting

Case inside case?

Im new to unix and shell scripting. I am required to write a program and im in the process of creating a menu system. I have my main menu but i want to be able to select an option that takes me onto another menu. I have tried doing this with the case statement with no luck so far. Wondering if it... (3 Replies)
Discussion started by: RAFC_99
3 Replies

9. Shell Programming and Scripting

Nested Case in UNIX script

Hi I wanted to know if we can write a nested case in UNIX script. Something like following - Case ${sDB} in Srvr1) case ${sSchema} Sch1) DBusr=Username1 DBPwd=Pwd1 ;; Sch2) DBusr=Username2 ... (1 Reply)
Discussion started by: sumeet
1 Replies

10. Shell Programming and Scripting

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 >... (9 Replies)
Discussion started by: abdulaziz
9 Replies
Login or Register to Ask a Question