0

ファイルからいくつかのデータを読み取り、必要な 2 つの列だけに grep を使用して、出力を変数にリダイレクトしました。

私のスクリプトは次のようになります。

#!/bin/bash
cat hosts.cfg | grep 'address\|host_name' | sed -e 's/\<address\>//g'  | while read line; do
        echo $line | sed 's/host_name//g' | sed -r 's/\s+//g' ;
done

出力は次のようになります。

Host1
xx.xx.xx.xx
Host2
xx.xx.xx.xx

問題は、hosts と ips をファイルではなく配列に保存する必要があることです!

出力は次のようになります。

Host1(tab)xx.xx.xx.xx

Host2(tab)xx.xx.xx.xx
4

4 に答える 4

0

使用してsed 'N;s/\n/\t/g'

変化する

Host1
xx.xx.xx.xx
Host2
xx.xx.xx.xx

Host1(tab)xx.xx.xx.xx
Host2(tab)xx.xx.xx.xx
于 2013-10-14T08:46:20.153 に答える
0

「set」を使用して、より高速な出力を得ることができます

例:

私はファイルbest2を持っています、

 # cat best2
 Host1 xx.xx.xx.xx Host2 xx.xx.xx.xx

次のスクリプトを作成します: tabcheck.sh

 # cat tabcheck.sh
 #!/bin/bash
 out=$(cat best2)
 set $out
 echo -e "$1\t$2\n$3\t$4"

 # ./tabcheck.sh
 Host1   xx.xx.xx.xx
 Host2   xx.xx.xx.xx

shift コマンドを使用する場合 (通常、位置パラメーターを使用してアクセスできるコマンド ライン引数は 9 つだけです。shift コマンドを使用すると、各引数をシフトすることで、9 つを超えるコマンド ライン引数にアクセスできます)。

ありがとう。

于 2013-10-15T07:27:36.247 に答える
0

You can use awk:

echo $output | awk 'NR%2{printf $0"\t";next;}1'

To save any command output, wrap it in backticks, or the newer (but less backward compatible) $(command) style substitution. E.g.:

result=`echo $output | awk 'NR%2{printf $0"\t";next;}1'`
于 2013-10-14T08:16:57.133 に答える