简体   繁体   中英

How to remove the carriage return and replace it with a space in a text file?

I am reading from a text file so what i want is: to remove that carriage return and add a space between the words.

I tried this but i am getting an error

 $text =~ s/\n+/\s/g;

替换的右边是一个字符串,而不是正则表达式,因此您放置了一个文字空间而不是\\s

$text =~ s/\n+/ /g;

Is like Zaid said.

If you want to use a more powerful (crossplatform) regex, i suggest this one

(\r?\n|\r\n?)+

So your code becomes:

$text =~ s/(\r?\n|\r\n?)+/ /g;

if you wanna do it with a one-line command, you could do:

perl -pi -e 's/(\r?\n|\r\n?)+/ /g' your_file

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