[英]Appending text to end of line with sed - shell script
我有一個簡單的shell腳本,幾乎可以按需工作了。
到目前為止,已達到要求,但似乎無法正確地將其附加到一行。
例如,這是我的腳本:
#!/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
通過從-e> -i更改sed標志來解決此問題,並刪除了對tmp文件的使用,並使用sed進行了處理:
#!/bin/bash
shopt -s nullglob
FILES=(/etc/openvpn/TorGuard.*)
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)
sed -i $line's/$/ creds.txt &/' "$file"
done
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.