使用sed在多行中插入脚本的变量输出
insert variable output of script in multiple lines using sed
测试文件包含
$ cat test
i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15
i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10
脚本输出:
$ for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done
10.10.0.68
10.10.0.96
使用 sed 插入变量产生以下结果,注意相同的 IP 地址
$ insert=( `for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done` )
$ sed "s|$|,${insert}|" test
i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15,10.10.0.68
i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10,10.10.0.68
但我正在寻找如下输出:
10.10.0.68,i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15
10.10.0.96,i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10
使用开始分隔符 ^
而不是结束 $
并调整 ,
sed "s/^/${insert},/" test
但是您的 sed 和值检索需要进入循环,而不是在所有结果之后或将所有结果作为值
循环中的示例:
for instance_id in $(cut -d"," -f1 test)
do
insert="$( python getattrib.py get ${instance_id} | cut -d"'" -f2 )"
sed -e "/^${instance_id}/ !d" -e "s|$|,${insert}|" test
done
insert=( `for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done` )
insert
变量是一个 数组,包含 2 个元素
sed "s|$|,${insert}|" test
${insert}
仅检索 first 元素——它是隐式的 ${insert[0]}
我会这样重写,逐行读取文件:
while IFS=, read -ra fields; do
ip=$( python getattrib.py get "${fields[0]}" | cut -d"'" -f2 )
printf "%s" "$ip"
printf ",%s" "${fields[@]}"
echo
done < test
测试文件包含
$ cat test
i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15
i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10
脚本输出:
$ for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done
10.10.0.68
10.10.0.96
使用 sed 插入变量产生以下结果,注意相同的 IP 地址
$ insert=( `for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done` )
$ sed "s|$|,${insert}|" test
i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15,10.10.0.68
i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10,10.10.0.68
但我正在寻找如下输出:
10.10.0.68,i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15
10.10.0.96,i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10
使用开始分隔符 ^
而不是结束 $
并调整 ,
sed "s/^/${insert},/" test
但是您的 sed 和值检索需要进入循环,而不是在所有结果之后或将所有结果作为值
循环中的示例:
for instance_id in $(cut -d"," -f1 test)
do
insert="$( python getattrib.py get ${instance_id} | cut -d"'" -f2 )"
sed -e "/^${instance_id}/ !d" -e "s|$|,${insert}|" test
done
insert=( `for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done` )
insert
变量是一个 数组,包含 2 个元素
sed "s|$|,${insert}|" test
${insert}
仅检索 first 元素——它是隐式的 ${insert[0]}
我会这样重写,逐行读取文件:
while IFS=, read -ra fields; do
ip=$( python getattrib.py get "${fields[0]}" | cut -d"'" -f2 )
printf "%s" "$ip"
printf ",%s" "${fields[@]}"
echo
done < test