在没有正则表达式的文件中搜索子字符串然后替换行
Search sub string then Replace Line in a file without regex
我想要 运行 一个脚本来搜索 /etc/bash.bashrc 文件中的子字符串
PS1=
并将整行替换为:
PS1='\[\e[36m\]\h\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[33m\]\u\[\e[m\]:\[\e[32m\]\W\[\e[m\]>\$ '
这一新行旨在更改 cli 提示符。
我已经在 bash 脚本中尝试了 sed 但我无法正确使用正则表达式。
[编辑] 此代码现在有效:
#!/bin/bash
custom_prompt='${debian_chroot:+($debian_chroot)}\[\e[36;40m\]\u\[\e[m\]\[\e[93m\]@\[\e[m\]\[\e[36m\]\h\[\e[m\]:\[\e[92m\]\w\[\e[m\]\[\e[92m\]\$\[\e[m\]\[\e[93m\]>\[\e[m\]\'
### Setup Bash Prompt
# replace each \ for double \ in the prompt string
sed_custom_prompt=$(<<<"$custom_prompt" sed 's/\/\\/g')
# add this to /etc/bashrc for global effect
sed -i "s/PS1=.*/PS1=\"$sed_custom_prompt\"/" testrc
唯一的问题是它 PS1= " string "
而不是 PS1 = ' string '
背部抽搐。
我需要一个简单的老式非正则表达式脚本来查找字符串并替换文件中的一行。正则表达式可以找到字符串,但我的原始语句弄乱了替换。
我不关心它是 perl、awk 还是 bash。我只需要有用的东西。
您应该每隔 \
转义一次,以确保它们不会丢失。
编辑:PS1 字符串也应该用双引号引起来。
$ custom_prompt="\[\e[36m\]\h\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[33m\]\u\[\e[m\]:\[\e[32m\]\W\[\e[m\]>\$ "
$ sed_custom_prompt=$(<<<"$custom_prompt" sed 's/\/\\/g')
$ sed -i "s/PS1=.*/PS1=\"$sed_custom_prompt\"/" testrc
$ source testrc
laptop@user:~>$
以下代码适用于我的笔记本电脑。问题出在 PS1
变量字符串中的最后一个 \
字符(我删除了它):
#! /bin/bash
custom_prompt='${debian_chroot:+($debian_chroot)}\[\e[36;40m\]\u\[\e[m\]\[\e[93m\]@\[\e[m\]\[\e[36m\]\h\[\e[m\]:\[\e[92m\]\w\[\e[m\]\[\e[92m\]\$\[\e[m\]\[\e[93m\]>\[\e[m\] '
### Setup Bash Prompt
# replace each \ for double \ in the prompt string
sed_custom_prompt=$(<<<"$custom_prompt" sed 's/\/\\/g')
# add this to /etc/bashrc for global effect
sed -i "s/PS1=.*/PS1='$sed_custom_prompt'/" testrc
exit 0
p.s。我个人喜欢将时间添加到 PS1 中,这样我就知道命令退出了多长时间。此外,如果添加它,您可以立即计时 (\D{%H}:\D{%M}
)。
试试这个:
replace='PS1="\[\e[36m\]\h\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[33m\]\u\[\e[m\]:\[\e[32m\]\W\[\e[m\]>\$ "'
perl -i -spe 's/^PS1=.*$/$repl/' -- -repl="$replace" -- /etc/bash.bashrc
注意:也许在第一个 运行 中删除 -i
(就地编辑),检查它是否有效。
与其编写脚本来替换现有的PS1
,不如简单地覆盖它。
echo PS1="This is my prompt" >> /etc/bash.bashrc
这会将新的 PS1 附加到文件的末尾,因为这最终会覆盖默认的 PS1 初始化。
我想要 运行 一个脚本来搜索 /etc/bash.bashrc 文件中的子字符串
PS1=
并将整行替换为:
PS1='\[\e[36m\]\h\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[33m\]\u\[\e[m\]:\[\e[32m\]\W\[\e[m\]>\$ '
这一新行旨在更改 cli 提示符。
我已经在 bash 脚本中尝试了 sed 但我无法正确使用正则表达式。
[编辑] 此代码现在有效:
#!/bin/bash
custom_prompt='${debian_chroot:+($debian_chroot)}\[\e[36;40m\]\u\[\e[m\]\[\e[93m\]@\[\e[m\]\[\e[36m\]\h\[\e[m\]:\[\e[92m\]\w\[\e[m\]\[\e[92m\]\$\[\e[m\]\[\e[93m\]>\[\e[m\]\'
### Setup Bash Prompt
# replace each \ for double \ in the prompt string
sed_custom_prompt=$(<<<"$custom_prompt" sed 's/\/\\/g')
# add this to /etc/bashrc for global effect
sed -i "s/PS1=.*/PS1=\"$sed_custom_prompt\"/" testrc
唯一的问题是它 PS1= " string "
而不是 PS1 = ' string '
背部抽搐。
我需要一个简单的老式非正则表达式脚本来查找字符串并替换文件中的一行。正则表达式可以找到字符串,但我的原始语句弄乱了替换。
我不关心它是 perl、awk 还是 bash。我只需要有用的东西。
您应该每隔 \
转义一次,以确保它们不会丢失。
编辑:PS1 字符串也应该用双引号引起来。
$ custom_prompt="\[\e[36m\]\h\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[33m\]\u\[\e[m\]:\[\e[32m\]\W\[\e[m\]>\$ "
$ sed_custom_prompt=$(<<<"$custom_prompt" sed 's/\/\\/g')
$ sed -i "s/PS1=.*/PS1=\"$sed_custom_prompt\"/" testrc
$ source testrc
laptop@user:~>$
以下代码适用于我的笔记本电脑。问题出在 PS1
变量字符串中的最后一个 \
字符(我删除了它):
#! /bin/bash
custom_prompt='${debian_chroot:+($debian_chroot)}\[\e[36;40m\]\u\[\e[m\]\[\e[93m\]@\[\e[m\]\[\e[36m\]\h\[\e[m\]:\[\e[92m\]\w\[\e[m\]\[\e[92m\]\$\[\e[m\]\[\e[93m\]>\[\e[m\] '
### Setup Bash Prompt
# replace each \ for double \ in the prompt string
sed_custom_prompt=$(<<<"$custom_prompt" sed 's/\/\\/g')
# add this to /etc/bashrc for global effect
sed -i "s/PS1=.*/PS1='$sed_custom_prompt'/" testrc
exit 0
p.s。我个人喜欢将时间添加到 PS1 中,这样我就知道命令退出了多长时间。此外,如果添加它,您可以立即计时 (\D{%H}:\D{%M}
)。
试试这个:
replace='PS1="\[\e[36m\]\h\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[33m\]\u\[\e[m\]:\[\e[32m\]\W\[\e[m\]>\$ "'
perl -i -spe 's/^PS1=.*$/$repl/' -- -repl="$replace" -- /etc/bash.bashrc
注意:也许在第一个 运行 中删除 -i
(就地编辑),检查它是否有效。
与其编写脚本来替换现有的PS1
,不如简单地覆盖它。
echo PS1="This is my prompt" >> /etc/bash.bashrc
这会将新的 PS1 附加到文件的末尾,因为这最终会覆盖默认的 PS1 初始化。