如何使用/替换带有sed的行

时间:2015-05-05 20:33:01

标签: regex linux sed

您好我目前有这种格式的文件:

This  is a test file

location = /home/files

我的问题是如何将此行location = /home/files替换为此location = /home/documents/test_files

2 个答案:

答案 0 :(得分:3)

如果原始字符串中有“/”或替换字符串,我们会使用“\”来转义“/”。或者,您可以使用任何字符作为替换分隔符,例如“@”

$ cat sample.csv 
This  is a test file

location = /home/files

$ sed 's@/home/files@/home/documents/test_files@' sample.csv 
This  is a test file

location = /home/documents/test_files

答案 1 :(得分:1)

只需选择不同的分隔符:

sed 's-location = /home/files-location = /home/documents/test_files-' test.txt
sed 's~location = /home/files~location = /home/documents/test_files~' test.txt

或逃避/

sed 's/location = \/home\/files/location = \/home\/documents\/test_files/' test.txt