如何用 sed 替换一行 /
How do I replace a line with sed with /
您好,我目前有一个这种格式的文件:
This is a test file
location = /home/files
我的问题是如何用 location = /home/documents/test_files
替换这一行 location = /home/files
?
当原始字符串或替换字符串中有“/”时,我们想用“\”转义“/”。或者,您可以使用任何字符作为替换分隔符,例如“@”
$ 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
只需选择不同的分隔符:
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
您好,我目前有一个这种格式的文件:
This is a test file
location = /home/files
我的问题是如何用 location = /home/documents/test_files
替换这一行 location = /home/files
?
当原始字符串或替换字符串中有“/”时,我们想用“\”转义“/”。或者,您可以使用任何字符作为替换分隔符,例如“@”
$ 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
只需选择不同的分隔符:
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