用换行符替换字符组
Replace groups of characters by newline
我的文件中有一行 ', '
,我想用 new line
替换它
输入:
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)', 'siteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754', '
[...]
我的sed命令:
sed "s/\', \'/\n/g"
输出:
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)nsiteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754n
在我的输出中,换行符已替换为字符 n
为什么?
您可以像这样使用 sed
来使用 \n
代替:
sed "s/', '/"$'\\n'"/g" file
这里我们使用$'\n'
来替换一个换行符。我们最终使用了 ``$'\\n'due to use of double quotes around
sed` 命令。
根据man bash
:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard
否则多行 sed
:
sed "s/', '/\
/g" file
这将适用于 bash 上的 gnu 和 POSIX sed 版本。
PS:如果您使用的是 gnu sed
,则简化命令为:
sed "s/', '/\n/g" file
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)
siteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754
[...]
我的文件中有一行 ', '
,我想用 new line
输入:
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)', 'siteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754', '
[...]
我的sed命令:
sed "s/\', \'/\n/g"
输出:
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)nsiteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754n
在我的输出中,换行符已替换为字符 n
为什么?
您可以像这样使用 sed
来使用 \n
代替:
sed "s/', '/"$'\\n'"/g" file
这里我们使用$'\n'
来替换一个换行符。我们最终使用了 ``$'\\n'due to use of double quotes around
sed` 命令。
根据man bash
:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard
否则多行 sed
:
sed "s/', '/\
/g" file
这将适用于 bash 上的 gnu 和 POSIX sed 版本。
PS:如果您使用的是 gnu sed
,则简化命令为:
sed "s/', '/\n/g" file
['siteed01pg|10.229.16.153|10.229.0.0|19|test / crt|BACKUP_MUT_SD Vlan981 (PVLAN 1981) New Backup Subnet #1 (site SD)
siteed01pg|10.129.135.53|10.129.135.0|26|test / crt|Fmer bopreprodback Vlan 754
[...]