Replacing Control Character in sed

后端 未结 5 1998
再見小時候
再見小時候 2020-12-01 03:29

I need to replace all occurrences of the control character CTRL+A (SOH/ascii 1) in a text file in linux, how can this be achieved in SED?

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 03:55

    By "replace", I'm assuming you want to change the file 'in-place'.

    Using GNU sed:

    # Create a file with a single control character (SOH)
    echo -e "\x01" > file
    # Change SOH control characters to the literal string "SOH"
    sed -i 's/\x01/SOH/g' file
    # Check result
    cat file
    

    gives...

    SOH
    

    The -i option doesn't work on OS X sed, so you'd need to work-around that by piping sed to a temporary file.

提交回复
热议问题