gpt4 book ai didi

linux - 使用 linux 从文件中加入行

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:02 25 4
gpt4 key购买 nike

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

最佳答案

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)

关于linux - 使用 linux 从文件中加入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17049752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com