我想把一行分成几个词。我知道这是可以做到的
For word in $line; do echo $word; done 但是我想做一个3-3个单词的组。所以我的问题是,我如何将一行分成3-3个单词的组?
例如
Input : I am writing this line for testing the code.
Output :
I am writing
this line for
testing the code. 发布于 2012-06-26 20:47:38
作为开始,您可以使用this,它将每个单词读入一个数组
#!/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下一步是使用seq或类似的方法对数组进行循环,并以三个一组的形式打印出来。
发布于 2012-06-26 22:37:11
一次读三个单词。将正在读取的行设置为余数:
while read -r remainder
do
while [[ -n $remainder ]]
do
read -r a b c remainder <<< "$remainder"
echo "$a $b $c"
done
done < inputfile发布于 2012-06-26 21:04:19
粘贴命令怎么样?
for word in $line; do echo $word; done | paste - - -
for word in $line; do echo $word; done | paste -d" " - - -https://stackoverflow.com/questions/11207102
复制相似问题