bash脚本 文件_在Bash脚本中,如何连接文件中的多行?

bash脚本 文件

In Bash script, how to join multiple lines from a file? For example, to join the lines

在Bash 脚本中 ,如何将一个文件中的多行连接起来? 例如加入行列

a
good
boy

to

a good boy

You can use tr command, something like:

您可以使用tr命令,例如:

tr -s 'n' ' ' < file.txt

tr -s 'n' ' ' < file.txt

It just goes through its input and makes changes according to what you specify in two sets (‘n’ ‘ ‘ in this case, we change each newline to a space), -s is there, in case of empty lines (more newline) which will be squeezed into one space.
Also you need to use redirection < as tr by default does’t take file as an argument.

它只是根据输入内容进行更改,并根据您在两组中指定的内容进行更改(在本例中为'n''',我们将每个换行符更改为一个空格 ),如果有空行,则-s存在(更多换行符)它将被压缩到一个空间中。
另外,您需要使用重定向<,因为默认情况下 tr不带file作为参数。

Answered by Lukáš Šníder.
卢卡什·斯尼德(Lukᚊníder)回答。


Another method using paste:

另一种使用paste方法:

paste -sd ' ' < file.txt

paste -sd ' ' < file.txt

翻译自: https://www.systutorials.com/in-bash-script-how-to-join-multiple-lines-from-a-file/

bash脚本 文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值