sed:如何用“\r"替换 CR 和/或 LF;“\n",所以任何文件都在一行 [英] sed: how to replace CR and/or LF with "\r" "\n", so any file will be in one line

查看:74
本文介绍了sed:如何用“\r"替换 CR 和/或 LF;“\n",所以任何文件都在一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的文件

啊啊

bbs

抄送

我需要它们进入 aaa\r\nbbb\r\nccc

它应该适用于 unix 和 windows,相应地用 \r\r\n 替换它们

It should work either for unix and windows replacing them with \r or \r\n accordingly

问题是 sed 在行尾添加了 \n 但保持行分隔.我该如何解决?

The problem is that sed adds \n at the end of line but keeps lines separated. How can I fix it?

推荐答案

这两个命令一起应该可以做你想做的:

These two commands together should do what you want:

sed ':a;N;$!ba;s/\r/\\r/g'
sed ':a;N;$!ba;s/\n/\\n/g'

通过两者传递您的输入文件以获得您想要的输出.可能有一种方法可以将它们组合成一个表达式.

Pass your input file through both to get the output you want. Theres probably a way to combine them into a single expression.

从这个问题中窃取和修改:

Stolen and Modified from this question:

如何使用 sed 替换换行符 (\n)?

这篇关于sed:如何用“\r"替换 CR 和/或 LF;“\n",所以任何文件都在一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆