Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何用sed替换一行/_Regex_Linux_Sed - Fatal编程技术网

Regex 如何用sed替换一行/

Regex 如何用sed替换一行/,regex,linux,sed,Regex,Linux,Sed,您好,我目前有一个以下格式的文件: This is a test file location = /home/files 我的问题是如何将此行位置=/home/files替换为this location=/home/documents/test\u files?当原始字符串或替换字符串中有/时,我们使用\来转义/。或者,您可以使用任何字符作为替换分隔符,例如@ $ cat sample.csv This is a test file location = /home/files $

您好,我目前有一个以下格式的文件:

This  is a test file

location = /home/files

我的问题是如何将此行位置=/home/files替换为this location=/home/documents/test\u 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

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

$ 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
sed 's~location = /home/files~location = /home/documents/test_files~' test.txt
或者逃离这个世界:


这是测试文件的一部分吗?档案里还有别的东西吗。你试过做这件事吗。本网站上有大量关于更改文件的问题。是的,这是一个测试文件,是文件的一部分,而文件中没有其他内容。这是文件的一部分吗?档案里还有别的东西吗。你试过做这件事吗。本网站上有大量关于更改文件的问题。是的,这是一个测试文件,是文件的一部分,文件中没有其他内容。