Optimize multiple awk variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimize multiple awk variable assignment
# 1  
Old 09-01-2016
Optimize multiple awk variable assignment

how can i optimize the following:

Code:
TOTALRESULT="total1=4
total2=9
total3=89
TMEMORY=1999"

Code:
        TOTAL1=$(echo "${TOTALRESULT}" | egrep "total1=" | awk -F"=" '{print $NF}')
        TOTAL2=$(echo "${TOTALRESULT}" | egrep "total2=" | awk -F"=" '{print $NF}')
        TOTAL3=$(echo "${TOTALRESULT}" | egrep "total3=" | awk -F"=" '{print $NF}')
        TMEMORY=$(echo "${TOTALRESULT}" | egrep "tmemory=" | awk -F"=" '{print $NF}')

i would like to, if possible, avoid the multiple egrep and multiple awk.
# 2  
Old 09-01-2016
Code:
typeset -u TOTALRESULT

TOTALRESULT="total1=4
total2=9
total3=89
TMEMORY=1999"

eval "$TOTALRESULT"

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 09-01-2016
Note that typeset -u (values are uppercase) is ksh.
eval can be evil. A bit more careful is
Code:
eval $(
echo "$TOTALRESULT" | awk '/^[[:alnum:]_]+=[[:print:]]*$/'
)

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 09-01-2016
You could also consider:
Code:
#!/bin/ksh
IAm=${##*/}
TmpFile="$IAm".$$

trap 'rm -f "$TmpFile"' EXIT

TOTALRESULT="total1=4
total2=9
total3=89
string=abc=def
TMEMORY=1999"

awk '
BEGIN {	FS = OFS = "="
}
{	$1 = toupper($1)
}1' <<EOF > "$TmpFile"
$TOTALRESULT
EOF

. "$TmpFile"

printf '%s is set to %s\n' \
	TOTAL1 "$TOTAL1" \
	TOTAL2 "$TOTAL2" \
	TOTAL3 "$TOTAL3" \
	TMEMORY "$TMEMORY" \
	STRING "$STRING"

which produces the output:
Code:
TOTAL1 is set to 4
TOTAL2 is set to 9
TOTAL3 is set to 89
TMEMORY is set to 1999
STRING is set to abc=def

Although written and tested using a 1993+ version of the Korn shell, this script should work with any POSIX-conforming shell.
# 5  
Old 09-02-2016
In my bash 4, this seems to work:
Code:
. <(echo "${TOTALRESULT^^}")
echo $TOTAL1, $TMEMORY 
4, 1999

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimize awk code

sample data.file: 0,mfrh_green_screen,1454687485,383934,/PROD/G/cicsmrch/sys/unikixmain.log,37M,mfrh_green_screen,28961345,0,382962--383934 0,mfrh_green_screen,1454687785,386190,/PROD/G/cicsmrch/sys/unikixmain.log,37M,mfrh_green_screen,29139568,0,383934--386190... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Optimize awk command

WARNING=${1} CRITICAL=${2} echo ${OUTPUT} | gawk -F'' ' { V = $2 R = $0 } END { for ( i = 1; i <= n; i++) { if((V > 0) && (V < V)) print R, ((V - V) / V) * 100 else if ((V > V) && (V > 0)) ... (6 Replies)
Discussion started by: SkySmart
6 Replies

4. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

5. Shell Programming and Scripting

awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy! Seriously, though, is there any practical difference? I was... (3 Replies)
Discussion started by: treesloth
3 Replies

6. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

7. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

8. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

9. Shell Programming and Scripting

awk variable assignment in a file

Hi All, I have a little awk script which uses a variable (x): awk -v x=0 'NF != 6 { ++x } END { print "This batch had " x " errors out of ", NR" records"}' But when I've tried to put the command in a file I can't seem to declare the variable. I've managed to simplify the code so that I... (4 Replies)
Discussion started by: pondlife
4 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question

Featured Tech Videos