Bash:将行拆分为多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36689275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:30:53 来源:igfitidea点击:
Bash: split line into multiple lines
提问by haael
I have a list of words in lines:
我有一个单词列表:
aaaa bbbb ccc dddd
eee fff ggg hhh
iii jjj kkk
I want each word in a separate line:
我希望每个单词都在一个单独的行中:
aaaa
bbbb
ccc
dddd
eee
fff
ggg
hhh
iii
jjj
kkk
How to do that in bash with least number of characters? Without awk preferably.
如何用最少的字符在 bash 中做到这一点?最好不用awk。
回答by fedorqui 'SO stop harming'
With pure bash:
使用纯 bash:
while IFS=" " read -r -a line
do
printf "%s\n" "${line[@]}"
done < file
See:
看:
$ while IFS=" " read -r -a line; do printf "%s\n" "${line[@]}"; done < file
aaaa
bbbb
ccc
dddd
eee
fff
ggg
hhh
iii
jjj
kkk
With xargs
:
与xargs
:
xargs -n 1 < file
With awk
:
与awk
:
awk '{for(i=1;i<=NF;i++) print $i}' file
or
或者
awk -v OFS="\n" '=' file
With sed
:
与sed
:
sed 's/ /\n/g' file
With cut
:
与cut
:
cut -d' ' --output-delimiter=$'\n' -f1- file
With grep
:
与grep
:
grep -o '[^ ]\+' file
or
或者
grep -Po '[^\s]+' file