Avatar of Watnog
Watnog
Flag for Belgium asked on

Issue with awk in cmd substitution with if

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.
* kshShell Scripting* AWK

Avatar of undefined
Last Comment
tel2

8/22/2022 - Mon
MURUGESAN N

@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

MURUGESAN N

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

ASKER CERTIFIED SOLUTION
MURUGESAN N

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Watnog

ASKER
Thank you very much. I was overcomplicating things, default space sep does the job.
Appreciated all your feedback.
Cheers.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
MURUGESAN N

$ echo MOST wELcOME | /usr/bin/wc >/dev/null
tel2

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

Open in new window


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

Open in new window

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

Open in new window

tel2

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

Open in new window

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

Open in new window

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.