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

时间:2021-03-03 19:30:24

Hello I currently have a file in this format:

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

This  is a test file

location = /home/files

My question is how do I replace this line location = /home/files with this location = /home/documents/test_files?

我的问题是如何用这个位置= / home / documents / test_files替换这个行位置= / home / files?

2 个解决方案

#1


When there's a "/" in the original string, or replacement string, we muse use "\" to escape "/". Alternatively you can use any character as substitution delimiter, such as "@"

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

$ 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

#2


Just choose a different delimiter:

只需选择不同的分隔符:

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

Or escape the /:

或逃避/:

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

#1


When there's a "/" in the original string, or replacement string, we muse use "\" to escape "/". Alternatively you can use any character as substitution delimiter, such as "@"

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

$ 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

#2


Just choose a different delimiter:

只需选择不同的分隔符:

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

Or escape the /:

或逃避/:

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