awk printing random "e" that is not in string

104 views Asked by At

I am wanting to extract ip and port from a string.

Strings look like this.

destination x.x.x.x:yyyy

where x is ip and y is port

    commandout=()
    while IFS= read -r line # Read a line
    do
        commandout+=("$line") # Append line to the array
    done < <(tmsh list ltm virtual $vip | grep destination)
    for output in "$commandout";
    do
        if [[ $output == *"destination"* ]];then
            #split off ip and port
            ipport=$(echo $output | awk 'BEGIN{}{print $2}')
            echo $ipport | awk 'BEGIN{FS=":"}{print $1}'
            echo $ipport
        fi
    done
    declare -p commandout

for some reason, awk is printing a random "e" after the ip address. But it only appears to do so after 2.

10.10.10.10
10.10.10.10:https
declare -a commandout='([0]="    destination 10.10.10.10:https")'
12.12.12.12e
12.12.12.12:https
declare -a commandout='([0]="    destination 12.12.12.12:https")'

UPDATE:

So I attempted another test. I found strange behavior and I am unsure how to fix it.

I declare the vipip before and after it is set.

    declare -p vipip
    vipip=$(tmsh list ltm virtual $vip | grep destination | awk 'BEGIN{}{print $2}' | awk 'BEGIN{FS=":"}{print $1}')
    echo $vipip
    declare -p vipip
    echo "cyle loop"

results in the following. Note that the 12.12.12.12 doesn't have an "e" on the end of it

./findvips-final.scr: line 240: declare: vipip: not found
10.10.10.10
declare -- vipip="10.10.10.10"
cyle loop
declare -- vipip="10.10.10.10"
12.12.12.12
declare -- vipip="12.12.12.12"
cyle loop

If I comment out the declare statements, I get an "e"

    #declare -p vipip
    vipip=$(tmsh list ltm virtual $vip | grep destination | awk 'BEGIN{}{print $2}' | awk 'BEGIN{FS=":"}{print $1}')
    echo $vipip
    #declare -p vipip
    echo "cyle loop"

results in

10.10.10.10
cyle loop
12.12.12.12e
cyle loop

I found the answer. I have a progress meter above this and I was getting the e off of complete.

echo -ne "$((100*$z/$count))% Complete\r"

I wrapped $vipip in qoutes on the echo and it is working like I thought. UGh wait a big waste of time.

1

There are 1 answers

2
Akshay Hegde On

You can straightaway set FS like below to extract Ip from your command, no need of loop, awk can search string also

your_command | awk -F'[ :]' '/destination/{gsub(/[^0-9.]/,"",$2); print $2}'

Explanation

-F'[ :]' - set field separator

'/destination/ - search for word destination in line/record/row

gsub(/[^0-9.]/,"",$2) - remove anything other than number and dot from second field ( so that random char like e, what you said above will be removed )

print $2 - print second field