我试图卷曲一个csv文件并根据其属性对其进行解析,然后使用变量名将其打印回去。
档案1:
10.0.0.1,gateway,name_of_device
10.2.4.5,server,name_of_device
10.3.5.6,PC,name_of_device
我下面的脚本
#!/bin/sh
input=$(curl http://example.com/1.txt)
ip=$(echo "$input" | awk -F ',' '{print $1}')
type=$(echo "$input" | awk -F ',' '{print $2}')
name=$(echo "$input" | awk -F ',' '{print $3}')
echo "$ip $type $name"
打印,
10.0.0.1
10.2.4.5
10.3.5.6
gateway
server
PC
name_of_device
name_of_device
name_of_device
但是预期的输出应该是这样,
10.0.0.1 gateway name_of_device
10.2.4.5 server name_of_device
10.3.5.6 PC name_of_device
尝试了不同的选择,例如
将输出分配给另一个变量并回显它,
# output="$source_ip $tag_text"
# echo -e $output
printf语句:
# printf "%-20s | %-20s" "$source_ip" "$tag_text"
# printf "$source_ip" "$tag_text"
awk当然是用于此目的的正确工具,但您也可以执行以下操作:
curl http://example.com/1.txt |
while IFS=, read ip type name rest; do
echo $ip $type $name
done
由于while循环位于子外壳中,因此变量将在while循环结束后丢失其值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句