当我从csv文件读取数据并存储在数组中时,我被卡在这里 现在我想根据条件显示数据
file_csv="ports.csv"
index=0
while IFS= read -r line;do
array["$index"]="$line"
index=$(($index + 1));
done < $file_csv
for lines in "${array[@]}"
do
#echo $(awk '/down/ {print}' | sed -n 's/HUAWEI//' <<< $index )
if [[ ${array[@]}=="down" ]]; then
echo "found $index" | sed 's/Huawei/d/'
fi
done
a=$(awk '/down/ {print}' $file_csv)
需要输出
Interface down down Summary
答案 0 :(得分:0)
要有效地将csv加载到数组:
mapfile array < "$file_csv"
如果您要执行的操作是仅存储带有down
的行:
array=( $( awk '/down/ {print}' "$file_csv" ) )
(使用您已经拥有的awk
,只需在其周围包裹另一组括号以使a
成为数组即可。)
您编写的循环以多种方式中断:
for lines in "${array[@]}" # iteratively assigns each element to $lines
do if [[ ${array[@]}=="down" ]] # [1] tests "down" against the unquoted entire set
then echo "found $index" | # [2] $index unset
sed 's/Huawei/d/' # Huawei not in "found $index"
fi
done
1:if
不太可能正常工作,并且可能会在语法上使程序崩溃,因为正在呈现数组的整个未引用内容以与“ down”进行比较,这可能与您期望的不一样整个数组包含...
2:根本没有设置“ $ index”。
也许这更接近您想要的?这样可以过滤掉华为记录。
array=( $( awk '/down/ && !/Huawei/ {print}' "$file_csv" ) )
如果您希望它不区分大小写-
array=( $( awk 'BEGIN{ IGNORECASE = 1 }; /down/ && !/Huawei/ {print}' "$file_csv" ) )
您稍后在代码中使用数组做其他事情吗?
因为如果没有,您最好跳过该数组,而只使用awk
。
您能详细说明吗?