0

我一直在寻找这样的东西。我有一个 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 或脚本可能会有所帮助

4

1 回答 1

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)
于 2013-06-11T17:51:17.477 回答