加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash中将一行分为单词

发布时间:2020-12-15 22:00:32 所属栏目:安全 来源:网络整理
导读:我想把一行分成几行.我知道这可以用这个完成 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 writingthis line
我想把一行分成几行.我知道这可以用这个完成

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.

解决方法

首先,您可以使用它,将每个单词读入数组

#!/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或类似的循环遍历数组并以三个为一组进行打印.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读