【发布时间】:2017-01-10 13:01:57
【问题描述】:
我正在尝试卷曲一个 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"
【问题讨论】:
标签: linux bash shell csv parsing