简体   繁体   中英

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. 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 -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 -wpe 'chomp if /,$/' file

This says: Print all the lines of "file", but leave out the newline if the line ends on a comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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