首页 文章

用文字\ n替换换行符

提问于
浏览
25

This stackoverflow question有一个答案,用sed替换换行符,格式为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"}]}]}

4 回答

  • 47

    这应该适用于 LFCR-LF 行结尾:

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

    这就是你要做的吗?

    $ 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 ,或者在printf之前使用带有GNU awk的 -v RS='\r?\n' 或者使用 sub(/\r$/,""); ,或者使用其他十几个简单明了的方法来处理它 .

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

  • 4

    你可以使用 sedtr 来做到这一点:

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

    但是这会在最后添加一个额外的 \n .

  • 17

    这里有一个小python脚本,用于以递归方式将'\ r \ n'替换为目录中的'\ r'导入os 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)
    

相关问题