鉴于
# this is programmatically generated... hard-coded here for this example
commands="\nthing1@this is thing 1!\nthing2@this is thing 2!"
while read -r line; do
cmd=$(printf "$line" | cut -d'@' -f1 | tr -d '\n')
desc=$(printf "$line" | cut -d'@' -f2 | tr -d '\n')
printf ' %-20s %s\n' "$cmd" "$desc"
done <<< "$commands"
我预期会输出以下内容。
thing1 this is thing1!
thing2 this is thing2!
相反,我得到了:
thing1thing2 this is thing1!this is thing2!
我做错了什么?
我想要读取换行符,以便可以在@
处进行剪切
和格式化输出
。
echo "$commands" | awk -F@ '{printf("%20s\t%s\n",$1, $2)}'
一次处理所有数据,而不是每行 ~8 个进程 ;-)。祝你好运。 - shellterecho -e
...但是,没想到会用到awk
。我不是 bash 大师,所以对于每行进程的滥用表示歉意。 :-o 你介意把你的解决方案发表为答案吗? - neezer\\
、n
还是真正的换行符来自动生成字符串? - ricicut
。让read
带着while IFS=@ read -r cmd desc _; do ...
为您解析即可。 - William Pursell\\
n
不是 换行符,因此read -r
会将整个字符串读取为一行(它本来就是一行)。这个事实,加上你的示例由于使用未转义的感叹号而无法正常工作,让我想知道你是否真的意味着那些要成为换行符。 - rici