Sponsored Content
Top Forums Shell Programming and Scripting awk - multiple and nested if-then-else Post 303038727 by jcdole on Thursday 12th of September 2019 10:52:52 AM
Old 09-12-2019
Quote:
Originally Posted by RudiC
Not clear. The result you present is exactly what one would expect once your code has been rearranged like

Code:
awk -v a="$ROW_ID_TO_SEARCH" '
        {if ($1 == a)   if ($2 == "") printf ("ID_NULL --> ROW_ID:%s\tVALUE:%s\n",      $1, $2)
           else         if ($2 != "") printf ("ID_NOLT_NULL --> ROW_ID:%s\tVALUE:%s\n", $1, $2)
    }
' file

If your search pattern is not found AND $2 holds a value, print that "ID_NOLT_NULL" - line.

What is your desire? What am I missing here?
Than k you for your quick posting.3


I have found this rules when googleing :
Code:
if(conditional-expression1)
    action1;
else if(conditional-expression2)
    action2;
else if(conditional-expression3)
    action3;
    .
    .
else
    action n;

What I would like to do if possible is ( don't take care about the {} syntax; it is just to explain my thought) :


Code:
if(conditional-expression1)
    { if(conditional-expression4)        action4 ;   

    else if(conditional-expression5)        action5 ;

    else if(conditional-expression2)
        { if(conditional-expression6)             action6 ;   

         else if(conditional-expression7)             action7 ; } }


    else if(conditional-expression2)
      action2;  
    else if(conditional-expression3)
       action3 ;
    .
    . }
else
    action n;

I would like to add a sub level block of if-then-else any where.

--- Post updated at 17:52 ---

Quote:
Originally Posted by RudiC
Not clear. The result you present is exactly what one would expect once your code has been rearranged like

Code:
awk -v a="$ROW_ID_TO_SEARCH" '
        {if ($1 == a)   if ($2 == "") printf ("ID_NULL --> ROW_ID:%s\tVALUE:%s\n",      $1, $2)
           else         if ($2 != "") printf ("ID_NOLT_NULL --> ROW_ID:%s\tVALUE:%s\n", $1, $2)
    }
' file

If your search pattern is not found AND $2 holds a value, print that "ID_NOLT_NULL" - line.

What is your desire? What am I missing here?

To explain what i want I am going to use pseudo code so forget missing ';' or missing {} :
Code:
awk -v a="$ROW_ID_TO_SEARCH" '
 {if ($1 == a)        # everything now depend of the fact that $1 == a
if ($2 == "") printf ("ID_NULL --> ROW_ID:%s\tVALUE:%s\n", $1, $2) # because $1 == a && $2 == "" else if ($2 != "") printf ("ID_NOLT_NULL --> ROW_ID:%s\tVALUE:%s\n", $1, $2) # because $1 == a && $2 != ""
} else { # now here $1 != a and then nothing to do }' INPUT_FILE

and the result should be now :
Code:

because the row id to search is not in the input file
This User Gave Thanks to jcdole For This Post:
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested Loop to Echo Multiple Arrays

I have three arrays which hold three elements each. I have a fourth array which contains the names of those three arrays. I'm having difficulty creating a nested loop that can loop through each array and echo their values. script #!/bin/ksh # array of locations (usa, london, australia)... (1 Reply)
Discussion started by: yongho
1 Replies

2. Shell Programming and Scripting

awk 2 delimiter nested

Hello All, This work could be very easy for you guys. I would really appreciate help. input file: output file: (Desired) What I am capable of doing: Command: cat inputfile | awk -F\| '{print "num="$1" value="$2" digits="$3" name1="$4" file="$5" code="$6}' > outputfile Result what I am... (5 Replies)
Discussion started by: onlyroshni
5 Replies

3. UNIX for Advanced & Expert Users

sed in awk ? or nested awk ?

Hey all, Can I put sed command inside the awk action ?? If not then can i do grep in the awk action ?? For ex: awk '$1=="174" { ppid=($2) ; sed -n '/$ppid/p' tempfind.txt ; }' tempfind.txt Assume: 174 is string. Assume: tempfind.txt is used for awk and sed both. tempfind.txt... (11 Replies)
Discussion started by: varungupta
11 Replies

4. UNIX for Dummies Questions & Answers

Nested if with multiple conditions

Deal Experts I am working on a script to find a date which is 7 days older and follwoing is my approach #!/bin/sh Yr=`date +"%Y"` Mn=`date +"%m"` Md=28 Da=`date +"%d"` echo $Yr echo $Mn echo $Da var1=$Yr$Mn$Da echo "before" $var1 if expr $Da > 7 then Da=`expr $Da - 7`... (3 Replies)
Discussion started by: sweetnsourabh
3 Replies

5. 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

6. Shell Programming and Scripting

Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated... (3 Replies)
Discussion started by: bashzipper
3 Replies

7. Shell Programming and Scripting

Nested awk Statements

Hello again everyone, yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is: BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\">" print "<Export>" } { x=1; print "<section name=\"Query" NR "\">"... (5 Replies)
Discussion started by: Parrakarry
5 Replies

8. Shell Programming and Scripting

Using shell command need to parse multiple nested tag value of a XML file

I have this XML file - <gp> <mms>1110012</mms> <tg>988</tg> <mm>LongTime</mm> <lv> <lkid>StartEle=ONE, Desti = Motion</lkid> <kk>12</kk> </lv> <lv> <lkid>StartEle=ONE, Source = Velocity</lkid> <kk>2</kk> </lv> <lv> ... (3 Replies)
Discussion started by: NeedASolution
3 Replies

9. Shell Programming and Scripting

awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk. The text file has a organization like so: Name Date Status Location (city, state, zip fields) Where each of these is on a separate line in... (1 Reply)
Discussion started by: kellyanneghj
1 Replies
All times are GMT -4. The time now is 10:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy