简体   繁体   English

如何使用linux命令删除新行

[英]How to remove new line using linux command

I have a file that contains data as follows: 我有一个包含数据的文件,如下所示:

text1, text2, text3,
text4
text5, text6, text7,
text8

I need to bring the text4, text8 to the previous line that ends with a comma. 我需要将text4,text8带到以逗号结尾的上一行。 How can I achieve this please ? 请问我该如何实现?

EDIT: Please, note that I have thousands of line. 编辑:请注意,我有数千行。 I need to automate the process. 我需要使过程自动化。

Try the following perl command: 尝试以下perl命令:

$ perl -p -e 's/,\n/, /g' file
text1, text2, text3, text4
text5, text6, text7, text8
> perl -pe 'if(/,\n/){$_=~s/\n//g}' temp
text1, text2, text3,text4
text5, text6, text7,text8

do it inplace : 就地执行:

perl -pi -e 'if(/,\n/){$_=~s/\n//g}' temp

Perl is probably the easiest tool: Perl可能是最简单的工具:

perl -wpe 'chomp if /,$/' file

This says: Print all the lines of "file", but leave out the newline if the line ends on a comma. 这说:打印“文件”的所有行,但是如果该行以逗号结尾,则省略换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM