我想把一行分成几个词。我知道这可以用这个来完成
For word in $line; do echo $word; done
Run Code Online (Sandbox Code Playgroud)
但我想制作一组 3-3 个单词。所以我的问题是,如何将一行分成 3-3 个单词的组?
例如
Input : I am writing this line for testing the code.
Output :
I am writing
this line for
testing the code.
Run Code Online (Sandbox Code Playgroud)
首先,您可以使用它,它将每个单词读入一个数组
#!/bin/bash
total=0
while read
do
for word in $REPLY
do
A[$total]=$word
total=$(($total+1))
done
done < input.txt
for i in "${A[@]}"
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
下一步是使用seq或类似的方法循环遍历数组并以三个为一组打印它。