this is my first post in Stackoverflow. I have an issue with executing sed command in the script while same command works fine separetly.
script:
#!/bin/bash
filename=file
sensitive="censored"
while IFS= read -r line; do
if [[ $line == *"Memory Region Size"* ]];
then
server_name=$(echo "$line" | awk '{print $11}')
echo "$line" >> test
elif [[ $(echo "$line" | wc -w) == 4 ]];
then
echo "$line" | awk '$4="censored"' >> test
else
echo "$line" >> test
fi
done < $filename
sed -i "s/${server_name}/${sensitive}/g" test
echo $server_name
echo $sensitive
I want to censor file with replacing server_name with "censored". This is output I am taking:
$ ./log_censor.sh
sed: -e expression #1, char 36: unterminated `s' command
ABC-123
censored
The same command it one-line command works fine for me:
$ cat file1
My server is ABC-123
$ server_name="ABC-123";sensitive="censored";sed -i "s/${server_name}/${sensitive}/g" file1
$ cat file1
My server is censored
I appreciate any help. Thanks
Mixing bash and awk and sed seems unnecessary. Do it all with one language, awk for example.
# usage: awk -f this_script.awk file file > test
BEGIN {sensitive = "censored"}
NR == FNR && /Memory Region Size/ {server_name = $11}
NR == FNR {next}
NF == 4 {$4 = sensitive}
{
gsub(server_name, sensitive)
print
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.