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作为参数。
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脚本 文件