在bash中剪切/回音
我对下面的剪切在bash脚本中的工作方式感到困惑在bash中剪切/回音,bash,echo,cut,Bash,Echo,Cut,我对下面的剪切在bash脚本中的工作方式感到困惑 文件.csv的示例: #10.10.10.10;28;9.10.10.10: up;Something ;More random spaces 我的剧本: #!/bin/bash csv_file="file.csv" locations=( $( cut -d';' -f5 $csv_file ) ) for ((i=0; i < ${#locations[@]}; i++)) do echo "${locations[$i
文件.csv的示例:
#10.10.10.10;28;9.10.10.10: up;Something ;More random spaces
我的剧本:
#!/bin/bash
csv_file="file.csv"
locations=( $( cut -d';' -f5 $csv_file ) )
for ((i=0; i < ${#locations[@]}; i++))
do
echo "${locations[$i]}"
done
当我只是在我的CLI中复制并粘贴剪切,而没有任何回音或变量时,剪切会按照我的预期工作并打印:
More random spaces
我确信这是一个括号或引号的问题,但我就是想不出来。下面的语句创建了一个包含三个元素的数组:
location=(More random spaces)
您的姓名和地址:
可以通过将命令替换用双引号括起来来防止这种情况:
arr=( "$(...)" )
echo "${arr[0]}" # hello world
这同样适用于,例如:
a=“你好,世界”
printf“$a”
printf““$a”
您需要引用位置数组中的subshell命令:
locations=( "$( cut -d';' -f5 $csv_file )" )
有关“带空格的数组”的更多信息,请参见此处:您的cut
命令提供字符串更多的随机空格
,当您将其转换为数组时,它有3个字段
您可以将脚本更改为
cut -d";" -f5 < ${csv_file}
如果需要整个字符串,为什么要将其保存到数组中?
a="hello world"
printf "<%s>" $a # <hello><world>
printf "<%s>" "$a" # <hello world>
locations=( "$( cut -d';' -f5 $csv_file )" )
cut -d";" -f5 < ${csv_file}
csv_file="file.csv"
while IFS=";" read -r f1 f2 f3 f4 f5 f6_and_higher; do
# Ignore fields f1 f2 f3 and f4
echo "${f5}"
done < ${csv_file}
awk -F ";" '{print $5}' ${csv_file}