用文字\ n替换换行符

时间:2016-07-30 10:06:40

标签: sed

This stackoverflow question可以使用sed':a; N; $!ba; s / \ n / / g'。

格式替换换行符。

这适用,但不适用于\ r,\ n等特殊字符

我尝试做的是用文字\ n替换换行符。 试过

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

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

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

但都无济于事。 Sed不断用换行符代替换行符....

思想?

在第一次回答后编辑:

为了完整起见,运行的命令是:

PostContent = cat $TextTable | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g'

其中TextTable是一个变量,链接到包含以下格式的JSON输出的文本文件:

{"posts":[{"title":"mysupertest","slug":"bi-test","markdown":"##TEST
First things first !
To TEST this TEST TEST, click the download button below.
If you need more information about the TEST TEST, you can  read the Table of Contents below.

<a href='/assets/TEST.pdf' style='border-radius:5px; padding: 4px 15px; background-color:#008CBA; color:white; text-decoration:none; float:right;' download> Download </a>


##TEST OF TEST


###TEST TEST PLATFORM TEST GUIDE
WaTESTve TEST SetupTEST
TESTTEST
TESTTESTETESTTETSTTEST
TESTTESTTTETST
TESTTES
TESTTESTESSTSTESTESTTES
TEST","image":"http://localhost:3000/myimage.jpg","featured":false,"page":false,"status":"draft","language":"en_US","meta_title":null,"meta_description":null,"author":"4","publishedBy":null,"tags":[{"uuid":"ember2034","name":"implementation guides","slug":null,"description":null,"meta_title":null,"meta_description":null,"image":null,"visibility":"public"}]}]}

6 个答案:

答案 0 :(得分:59)

这就是你想要的吗?

$ cat file
a
b
c

$ awk '{printf "%s\\n", $0}' file
a\nb\nc\n$

甚至:

$ awk -v ORS='\\n' '1' file
a\nb\nc\n$

首先在输入文件上运行dos2unix,如果你愿意,可以去掉\r,或者使用-v RS='\r?\n'和GNU awk,或者在printf或者其他任何一个之前使用sub(/\r$/,"");或者如此清晰,简单的处理方式。

sed用于单个行上的简单替换,即全部。对于其他任何你应该使用awk。

答案 1 :(得分:22)

这适用于LFCR-LF行结尾:

sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' file

答案 2 :(得分:5)

您可以使用sedtr执行此操作:

sed 's/$/\\n/' file | tr -d '\n'

但是,这会在最后添加额外的\n

答案 3 :(得分:2)

您可以使用-z选项

sed -z 's/\n/\\n/g' file

sed -z "s/\n/\\\n/g" file

答案 4 :(得分:0)

这里有一个用于替换&#39; \ r \ n&#39;的小python脚本。用&#39; \ r&#39;在目录中以递归方式     进口口     import sys

if len(sys.argv) < 2:
    print("Wrong arguments. Expected path to directory as arg 1.")
    exit(1)

path = sys.argv[1]


def RecOpOnDir(path, op) :
    for f in os.listdir(path):
        full_f = path + "/" + f
        if os.path.isdir(full_f):
            RecOpOnDir(full_f, op)
        else:
            try:
                op(full_f)
            except Exception as ex:
                print("Exception during proc '", full_f, "' Exception:", ex)

file_counter = 0

def WinEndingToUnix(path_to_file):
    global file_counter
    file_counter += 1
    file_strs = []
    with open(path_to_file) as f:
        for line in f:
            file_strs.append(line.replace(r"\r\n", r"\n"))

    with open(path_to_file, "w") as fw:
        fw.writelines(l for l in file_strs)

try:
    RecOpOnDir(path, WinEndingToUnix)
    print("Completed.", file_counter, "files was reformed")
except Exception as ex:
    print("Exception occured: ", ex)
    exit(1)

答案 5 :(得分:0)

万一它对任何人都有帮助,我正在寻找这个问题的反面:用换行符替换字符串中的文字'\'n。我设法用sed来解决这个问题:

_s="foo\nbar\n"
echo $_s | sed 's/\\n/\n/g'