bash --version:GNU bash,版本 5.0.17(1)-release (x86_64-pc-linux-gnu)
我的多行變數包含這個字串
item1 item2 item3 item4
item1 item2 item3 item4
item1 item2 item3 item4
我正在嘗試輸出 item3 (最終將其用于 API 呼叫,但現在只是回顯它)如下:
item3
item3
item3
這就是我嘗試這樣做的方式:
while IFS= read -r line
do
printf "${line[2]}"
done < <(printf '%s\n' "$multiline")
我目前一無所獲。表明(至少對我而言) printf 沒有得到一個陣列。但是當我用這樣的東西回圈時:
while IFS= read -r line
do
for item in ${line[@]}
do
echo "$item"
done
done < <(printf '%s\n' "$EC2")
它確實在單獨的行上回顯所有 12 個專案,就好像 $line 是一個合法的陣列一樣。我只想要每行的 item3。
uj5u.com熱心網友回復:
$ var='item1 item2 item3 item4
item1 item2 item3 item4
item1 item2 item3 item4'
$ echo "$var" | cut -d' ' -f3
item3
item3
item3
或者,如果您愿意:
$ while read -r _ _ foo _; do
echo "$foo"
done <<< "$var"
item3
item3
item3
或給出您的命令:
aws api call | cut -d' ' -f3
或者:
while read -r _ _ foo _; do
echo "$foo"
done < <(aws api call)
或者:
readarray -d $'\n' -t arr < <(aws api call)
printf '%s\n' "${arr[@]}" | cut -d' ' -f3
ETC....
uj5u.com熱心網友回復:
與cut -d' ' -f3方法類似,但使用awk:
$ echo 'item1 item2 item3 item4
item1 item2 item3 item4
item1 item2 item3 item4' | awk '{print $3}'
item3
item3
item3
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/466156.html
