ファイルからawk findに基づいてコマンドを実行する

ファイルからawk findに基づいてコマンドを実行する

awkを使用して、ファイル内の一致する文字列に基づいていくつかのコマンドを実行しようとしています。これが正しいアプローチかどうかはわかりません。 grepの使用はこの目的に適していますか?

#!/bin/bash
file1='ip.txt'
while read line; do 
  if `awk -F: '2 == /puppetclient/'` then 
    echo "Found the IP `awk '{print $1}'` with the text `awk '{print $2}'`"
    echo "Will install puppet agent"
  fi
  if `awk -F: '2 == /puppetmaster/'` then
    echo "Found the IP `awk '{print $1}'` with the text `awk '{print $2}'`"
    echo "Will install puppet server"
  fi
done < $file1

IP.txt

{
52.70.194.83 puppetclient
54.158.170.48 puppetclient
44.198.46.141 puppetclient
54.160.145.116 puppetmaster puppet
}

ベストアンサー1

awk直接使用するのではなく、ファイルを繰り返す理由が何であるかわかりません。

awk '
    /puppetclient/ {
        printf "Found the IP %s with the text %s\n", $1, $2
        printf "Will install puppet agent\n"
        system ("echo agent magic")    # Execute this command
    }
    /puppetmaster/ {
        printf "Found the IP %s with the text %s\n", $1, $2
        printf "Will install puppet server\n"
        system ("echo server magic")    # Execute this command
    }
' ip.txt

おすすめ記事