1

Have a file1 with vname and vport

Vname vport
xyc    3:4:7
sdf    5:5:5
sft    5:5:5
sfg    3:4:7
szd    1:2:3

Get the unique port

vport 1:2:3

and separate and assign each of them to a variable like a=1, b=2, c=3

3
  • 1
    It's not clear to me what are you trying to achieve. Can you give more detailed example?
    – mrc02_kr
    Nov 14, 2019 at 8:50
  • Do I understand you correctly that you are looking for a line starting with szd and then want to read the corresponding vport value into the three variables?
    – AdminBee
    Nov 14, 2019 at 8:51
  • When you say "get unique port", does that mean no a can be equal to a b or c? Or a/b/c should be unique regardless of each others?
    – SYN
    Nov 14, 2019 at 9:15

2 Answers 2

1

Does this answer your question?

#!/bin/bash

# IFS is a special enviroment variable. The character in it will be used
# by 'read' (which is a bash builtin command).
#
# In this case we need space and colon as separators
#
IFS=' :'

# Looping over lines in the file, fo each line read name, a, b, and c.

# "sed 1d" command deletes first line to skip over header ("Vname vport")
#
sed 1d file1 | while read name a b c ; do

    # If it was an empty line, skip and loop to next line
    #
    [ "$name" ] || continue

    # output the variables for demonstration
    #
    echo "name: $name"
    echo "a = $a"
    echo "b = $b"
    echo "c = $c"

    # extra line to separate output of next line
    #
    echo

done
2
  • I am curious: since you alread specified IFS, why don't you just use while read name a b c; do <...> done?
    – AdminBee
    Nov 14, 2019 at 10:23
  • No real reason, just me doing it overly complex :) Edited. Thanks.
    – Hkoof
    Nov 14, 2019 at 14:33
0

I assume that you want to identify that vport combination which only occurs once in the file, and want to split that into the three variables a, b and c. In this case, you could make use of associative arrays:

The following should work:

#!/bin/bash

declare -A counts   # declare `counts` as associative container

# Read the file line-wise
while read vname vport
do
    if [[ "$vname" == "Vname" ]]; then continue; fi # skip header

    # if current 'vport' value not yet encountered, add it to the array
    # with count=1, otherwise increment the value
    if [[ -z "${counts[$vport]}" ]]
    then
        counts[$vport]=1
    else
        let counts[$vport]=${counts[$vport]}+1
    fi
done < file.txt


# Identify which one only occurs once: iterate over all "keys" and
# check which "value" is "1"
found=0
for vp in "${!counts[@]}"
do
    # If such a key was found, split it at the ':' and read the parts
    # into individual variables
    if [[ "${counts[$vp]}" == "1" ]]
    then
        IFS=":"
        read a b c <<< "$vp"
        found=1
        break
    fi
done


# Output the variables if a unique port specification was found
if (( found == 1 ))
then
    echo "Unique port found: a=$a, b=$b, c=$c"
else
    echo "No unique port found!"
fi

Note that this assumes there is only one such unique port (which seems reasonable to guess from your example).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .