zoukankan      html  css  js  c++  java
  • sed 替换换行回车

    A carriage return linefeed (CRLF) is a special sequence of characters, used by DOS/Windows, to signify the end of a line of text. However, in *nix, only a linefeed (LF) is required to signify a line ending.

    test.CRLF.txt

    12345CRLF

    12345LF


    Delete/Replace a Carriage Return (CR) with sed

    There are a few ways to remove or replace a carriage return with sed.

        sed 's/.$//' test.CRLF.txt > test1.txt

    12345LF
    1234LF


        sed 's/x0D$//' test.CRLF.txt > test2.txt

    12345LF
    12345LF

        sed 's/ //' test.CRLF.txt > test3.txt

    12345LF
    12345LF



    The first method assumes all lines end with CRLF, while the second and third method depend on the version of sed.

    Delete/Replace a Linefeed (LF) with sed

    By default, sed strips the newline as the line is placed into the pattern space. However, the workaround is to read the whole file into a loop first.

    sed ':a;N;$!ba;s/ /<text>/g'

    where

    :a       - create a label 'a'
    N        - append the next line to the pattern space
    $!       - if not the last line
    ba       - branch (go to) label 'a'
    s        - substitute
    / /     - regex for new line
    /<text>/ - with text "<text>"
    g        - global match (as many times as it can)

    Example: Delete Newlines (CRLF) with sed

    Delete the carriage return first, and then delete the linefeed.

     
        sed -i 's/ //g' example.txt
        sed example delete newline carriage return.png
        sed -i ':a;N;$!ba;s/ /<text>/g' example.txt
        sed example delete newline linefeed.png

    tr: A Simpler Way to Replace or Delete a Newline (CRLF)

    The tr utility is a preferred and simpler method for replacing end of line characters as the pattern buffer is not limited like in sed.

    tr ' ' '<replace with text>' < input_file > output_file

    or to delete the newline (CRLF),

    tr -d ' ' < input_file > output_file

    where

    - carriage return
    - linefeed

    Example: Delete Newlines (CRLF) with tr
     
        tr -d ' ' < example.txt > example2.txt
        tr example delete newline CRLF.png

    Command Line Examples

    Note: The command cat will concatenate files and print on the standard output. The option A will use ^ and M notation to show non-printing characters, display $ at end of each line, and display TAB characters as ^I.

     

    REF:

    http://www.canbike.org/information-technology/sed-delete-carriage-returns-and-linefeeds-crlf.html

        ABHD12
        ABI2
        ACSM2A
        ACSM2B
        AGPAT5
        ANP32B
        APP
        ARID1A
        ARL10
        BACH2
        FBXL2
        FBXO22
        FBXO30
        FBXO39

  • 相关阅读:
    hdu 2485 Destroying the bus stations 迭代加深搜索
    hdu 2487 Ugly Windows 模拟
    hdu 2492 Ping pong 线段树
    hdu 1059 Dividing 多重背包
    hdu 3315 My Brute 费用流,费用最小且代价最小
    第四天 下载网络图片显示
    第三天 单元测试和数据库操作
    第二天 布局文件
    第一天 安卓简介
    Android 获取存储空间
  • 原文地址:https://www.cnblogs.com/emanlee/p/7703136.html
Copyright © 2011-2022 走看看