Differences between sed commands under Mac and Linux


question

(1) In Windows system, each line of the file ends with '<carriage return><line feed>', '\r\n'

(2) In Mac system, each line of the file ends with '<carriage return>', that is, '\r'

(3) In Unix systems, each line of the file ends with '<newline>', that is, '\n'

Therefore, if you use '\n' as the newline character in a file, there will be no newline when opened with Windows Notepad;

When files (windows files) that use '\r\n' as a newline character are opened with vim on Linux or Mac, \r will be displayed as ^M.


echo "a,b,c,d"

Under Mac or Linux system, the above results are all
a,b,c,d

Now use sed to replace "," with a newline, and execute it under the Linux system

echo "a,b,c,d" |sed 's/,/\n/g'.

The result is:


alt

The result of executing the same command under Mac is:

alt

solve


Solved by installing gsed,

brew install gnu-sed

Then use gsed,

alt

Add an alias to bash_profile and replace sed with gsed




For more related questions, please refer to [1]


Give it a try


sed -i 's#old path#new path#g' target file address


In the process of migrating from a virtual machine to a container on a large factory server, the location of the log and IP library files in the configuration file needs to be rewritten.

For example, you need to /home/google/log/service/qa/change the log_path in the original file from/home/appops/logs/service/qa/

implementsed -i 's#/home/google/log#/home/microsoft/logs#g' config.yml

(usually added in Makefile)

alt

For another example, it is necessary to

    city1: /中国/浙江/杭州/余杭/仓前.db
    city2: /中国/浙江/杭州/余杭/闲林.txt
    city3: /中国/浙江/杭州/余杭/中泰.md

Change to

    city1: /中国/余杭/仓前.db
    city2: /中国/余杭/闲林.txt
    city3: /中国/余杭/中泰.md

Can be executedsed -i 's#/中国/浙江/杭州#/中国#g' config.yml

alt

For another example, it is necessary to

    city1: /中国/浙江/杭州/余杭/街道/仓前.db
    city2: /中国/江苏/苏州/虎丘/街道/横塘.txt
    city3: /中国/山东/滨州/邹平/街道/黛溪.md

Change to

    city1: /中国/华东/街道/仓前.db
    city2: /中国/华东/街道/横塘.txt
    city3: /中国/华东/街道/黛溪.md

sure

sed -i 's#/中国/浙江/杭州/余杭/仓前.db#/中国/华东/余杭/仓前.db#g' config.yml
sed -i 's#/中国/江苏/苏州/虎丘/横塘.txt#/中国/华东/虎丘/横塘.txt#g' config.yml
sed -i 's#/中国/山东/滨州/邹平/黛溪.md#/中国/华东/邹平/黛溪.md#g' config.yml

But a better way is to use wildcards

sed -i 's#/中国/.*街道#/中国/华东/街道#g' config.yml

alt

in

  • . Represents any single character
  • * Indicates that a character appears 0 or more times

's#/中国/.*街#/中国/东华/街#g' means to replace 'China/xxxxxx any number of characters xxxxx street' with 'China/East China/street'


For more information, please refer to the coolshell-SED concise tutorial [2]

References

[1]

For more related questions, please refer to: https://www.google.com/search?newwindow=1&rlz=1C5CHFA_enCN758CN758&sxsrf=ACYBGNRgj_G2k3S1PNhmPd8Zzs-Jfyor2g%3A1574328575244&ei=_1jWXeHFDvTXz7sPn_KXWA&q=sed%E 5%91%BD%E4%BB%A4+mac+% E5%8C%BA%E5%88%AB&oq=sed%E5%91%BD%E4%BB%A4+mac+%E5%8C%BA%E5%88%AB&gs_l=psy-ab.3...295935.300081. .300238...4.2..0.311.2569.0j2j7j1...0....1..gws-wiz.....0i71j0i12j0i8i30j33i160.jDax8rOLIAk&ved=0ahUKEwihwfL7_vrlAhX063MBHR_5BQsQ4dUDCAs&uact=5

[2]

coolshell-SED brief tutorial: https://coolshell.cn/articles/9104.html

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/techdashen/article/details/132910158