We help IT Professionals succeed at work.

Issue with awk in cmd substitution with if

Watnog
Watnog asked
on
High Priority
48 Views
Last Modified: 2020-03-27
Dear Experts,

Can you please help me with this one?
i have below snippet...
I want to extract

session = MMECHJ-1
and
uproc =MMECHJ-1H


prompt>cat a
#!/usr/bin/ksh
set -x

LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"

if [[ $(echo $LINE | awk -F"-" '{ print $1"-" $2 }') == "MMECHJ-1" ]] ; then
session=$(echo "$LINE" | awk -F"-" '{ print $1"-"$2 }')
uproc=$(echo "$LINE" | awk -F"-" '{ print $3"-"$4 }' | awk -F";" '{ print $1 }')
fi


echo "$session"
echo "$uproc"

Open in new window



Below how it works out:
awk -F"-" '{ print $1"-" $2 }' results in "MMECHJ-1 MMECHJ" , while seperator is the dash ;;.

prompt>./a
+ LINE=MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904
+ awk -F- { print $1"-" $2 }
+ echo MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904
+ [[ MMECHJ-1 MMECHJ == MMECHJ-1 ]]
+ echo

+ echo

[bex1000@hn5219] /crelan/exp/prod/teste_production/teste_pascal/INC >

Open in new window


What's happening? This is ksh on aix.
:-~
Thanks again,
W.
Comment
Watch Question

MURUGESANApplication Development and Automation Manager
CERTIFIED EXPERT

Commented:
@Watnog

$ cat  29176129.sh
#!/usr/bin/ksh
LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"
echo "Value of LINE [$LINE]"
echo -n "Value of first column: "
echo "$LINE" | awk -F'-' '{ printf( "[%s]\n", $1); }'
echo -n "First column appended with -: "
echo "$LINE" | awk -F'-' '{ printf( "[%s]\n", $1"-"); }'
#We are using - as seperator and not space.
#Hence second column value is "1 MMECHJ"
echo -n "Second column: "
echo "$LINE" | awk -F'-' '{ printf( "[%s]\n", $2); }'
echo -n "First and second columns including -: "
echo "$LINE" | awk -F'-' '{ printf( "[%s-%s]\n", $1, $2); }'

Open in new window

output:
$ ./29176129.sh
Value of LINE [MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904]
Value of first column: [MMECHJ]
First column appended with -: [MMECHJ-]
Second column: [1 MMECHJ]
First and second columns including -: [MMECHJ-1 MMECHJ]

Open in new window

MURUGESANApplication Development and Automation Manager
CERTIFIED EXPERT

Commented:
Few more information:

$ cat  29176129.sh
#!/usr/bin/ksh
#export PS4='$0 $LINENO:'
#set -x
LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"
if [[ $(echo $LINE | awk -F"-" '{ print $1"-" $2 }') == "MMECHJ-1" ]]
then
        session=$(echo "$LINE" | awk -F"-" '{ print $1"-"$2 }')
        uproc=$(echo "$LINE" | awk -F"-" '{ print $3"-"$4 }' | awk -F";" '{ print $1 }')
        echo "$session"
        echo "$uproc"
else
        echo "Exception Value using awk:"
        echo $LINE | awk -F"-" '{ print $1"-" $2 }'
fi
$ ./29176129.sh
Exception Value using awk:
MMECHJ-1 MMECHJ

Open in new window

Application Development and Automation Manager
CERTIFIED EXPERT
Commented:
$ cat  29176129.sh
#!/usr/bin/ksh
#export PS4='$0 $LINENO:'
#set -x
LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"
#if [[ $(echo $LINE | awk -F"-" '{ print $1"-" $2 }') == "MMECHJ-1" ]]
#Modified above if condition removing -F option and $2
if [[ $(echo $LINE | awk '{ print $1 }') == "MMECHJ-1" ]]
then
        session=$(echo "$LINE" | awk '{ print $1 }')
        uproc=$(echo "$LINE" | awk '{ print $3"-"$4 }' | awk -F";" '{ print $1 }')
        echo "session $session"
        echo "uproc   $uproc"
else
        echo "Exception Value using awk:"
        echo $LINE | awk -F"-" '{ print $1"-" $2 }'
fi

Open in new window

output:
$ ./29176129.sh
session MMECHJ-1
uproc   P_PM43CEXP-EXECUTING

Open in new window

sample awk tutorial

https://www.tutorialspoint.com/awk/awk_workflow.htm

Author

Commented:
Thank you very much. I was overcomplicating things, default space sep does the job.
Appreciated all your feedback.
Cheers.
MURUGESANApplication Development and Automation Manager
CERTIFIED EXPERT

Commented:
$ echo MOST wELcOME | /usr/bin/wc >/dev/null
CERTIFIED EXPERT

Commented:
HI all,

As murugesandins has demonstrated, his final solution of outputs:
    session MMECHJ-1
    uproc   P_PM43CEXP-EXECUTING
but Watnog asked for:
    session = MMECHJ-1
and
    uproc =MMECHJ-1H
Did you adjust his code to compensate for that, Watnog?

And what's the point in doing this, with the ";" separator?:
    uproc=$(echo "$LINE" | awk '{ print $3"-"$4 }' | awk -F";" '{ print $1 }')
I don't see any ";" in $LINE.
Why not just this kind of thing?:
    uproc=$(echo $LINE | awk '{print $2}'

Or don't use awk at all, like this:
#!/usr/bin/ksh

LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"

set -- $LINE
if [[ $1 == "MMECHJ-1" ]]
then
  session=$1
  uproc=$2
fi
echo $session
echo $uproc

Or if you don't want to use "set -- ..." because you want to retain any parameters passed to the script, i.e. $1, $2, etc, how's this?:
#!/usr/bin/ksh

LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"

if [[ ${LINE%% *} == "MMECHJ-1" ]]
then
  session=${LINE%% *}
  uproc=$(echo $LINE | awk '{print $2}')
fi
echo $session
echo $uproc
Both of the above return the following (in OpenBSD at least):
    MMECHJ-1
    MMECHJ-1H

And in that last one, instead of awk:
    uproc=$(echo $LINE | awk '{print $2}')
you could use cut slightly more concisely:
    uproc=$(echo $LINE | cut -d" " -f2)

Alternatively, I don't know about ksh on AIX, but in bash you can do it with an array like this:
#!/bin/bash

LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"

ARR=($LINE)
if [[ ${ARR[0]} == "MMECHJ-1" ]]
then
  session=${ARR[0]}
  uproc=${ARR[1]}
fi
echo $session
echo $uproc
CERTIFIED EXPERT

Commented:
...but my personal favourite is to use a regular expression with bash:
#!/bin/bash 

LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"

regex='^(MMECHJ-1) ([^ ]*)'
if [[ $LINE =~ $regex ]]; then
  session=${BASH_REMATCH[1]}
  uproc=${BASH_REMATCH[2]}
fi
echo -e "$session\n$uproc"
Output:
    MMECHJ-1
    MMECHJ-1H

To make that slightly shorter:
# These lines:
regex='^(MMECHJ-1) ([^ ]*)'
if [[ $LINE =~ $regex ]]; then

# can be shortened to this:
if [[ $LINE =~ ^(MMECHJ-1)\ ([^ ]*) ]]; then

# or this:
if [[ $LINE =~ ^(MMECHJ-1)' '([^ ]*) ]]; then
See how the regex as a whole now can't be 'quoted', but that space in the middle of it had to be \escaped or 'quoted' to avoid a syntax error.