一意の値を見つけ、各値を別々の変数に抽出するシェルスクリプト

一意の値を見つけ、各値を別々の変数に抽出するシェルスクリプト

vnameとvportを含むfile1があります。

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

一意のポートを取得する

vport 1:2:3

これらを分離して、a = 1、b = 2、c = 3などの変数に割り当てます。

ベストアンサー1

これはあなたの質問に対する答えですか?

#!/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

おすすめ記事