我在找这样的东西。 我有一个demo.txt文件,其中包含以下单词
bold
code
make
links
placing
line
return
我愿意以
的方式实现结果bold
code
make
links
placing
line
return
bold code
code make
make links
links placing
placing line
line return
bold code make
code make links
make links placing
links placing line
placing line return
sed或awk或脚本可以提供帮助
答案 0 :(得分:2)
words_per_line() {
local n=$(($1-1)) i j
for ((i=0; i<${#words[@]}-n; i++)); do
for ((j=0; j<=n; j++)); do
printf "%s " ${words[i+j]}
done
echo
done
}
mapfile -t words < demo.txt # read the file into a zero-indexed array
words_per_line 1
words_per_line 2
words_per_line 3
输出
bold
code
make
links
placing
line
return
bold code
code make
make links
links placing
placing line
line return
bold code make
code make links
make links placing
links placing line
placing line return
这会产生相同的输出:
ruby -e '1.upto(3) {|n| ARGV.each_cons(n) {|a| puts a.join(" ")}}' $(< demo.txt)