bash - 使用 sed - shell 脚本将文本附加到行尾
问题描述
我有一个简单的 shell 脚本,我几乎可以按需要工作。
要求:
- 将 dir 中的文件名读入数组
- 遍历文件并将文本附加到匹配行的末尾
到目前为止,req one 已经实现,但我似乎无法让 sed 正确附加到一行。
例如,这是我的脚本:
#!/bin/bash
shopt -s nullglob
FILES=(/etc/openvpn/TorGuard.*)
TMPFILES=()
if [[ ${#FILES[@]} -ne 0 ]]; then
echo "####### Files Found #######"
for file in "${FILES[@]}"; do
echo "Modifying $file.."
line=$(grep -n "auth-user-pass" "$file" | cut -d: -f -1)
array=($(sed -e $line's/$/ creds.txt &/' "$file"))
tmp="${file/.conf/.ovpn}"
echo "$tmp created.."
TMPFILES+=("$tmp")
printf "%s\n" "${array[@]}" > ${tmp}
done
fi
预期输出:
....
....
auth-user-pass creds.txt
...
...
收到的输出:
...
...
auth-user-pass
creds.txt
...
...
解决方案
sed
使用特殊字符可能会很困难。在这种情况下,您可能会使用&
, 将由他完成的匹配字符串替换。
for file in "${FILES[@]}"; do
echo "Modifying ${file}.."
sed -i 's/.*auth-user-pass.*/& creds.txt/' "${file}"
done
推荐阅读
- r - 获得连续的数字而不重复
- python - 如何在不使用深度的情况下在子序列化程序中获取父模型?
- spring-boot - Spring Boot 应用程序中的条件自动装配
- php - 如何使现有的 Symfony 项目 --full?
- angular - Ag-grid 如何通过单击按钮更新单元格样式
- git - Git 钩子:“致命:不是 git 存储库:”
- postgresql - PostgreSQL 中所有数据库的只读用户
- express - Expressjs + ejs 和标签 href url 不适用于子目录路由
- python - 当一个 web 元素显示和消失两次时采用的逻辑
- kubernetes - Kubernetes(本地)Metalb LoadBalancer 和粘性会话