Issue
I have a text message in a bash script which I am trying to extract a part of it within a pattern. For example my text string looks like:
(cert_a='aaa',cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n')This is a sample text..
This is part of a script:
#!/bin/sh
test="(cert_a='aaa',cert_b='-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n')This is a sample text.."
sample_text=$test
echo $sample_text | busybox sed 's/cert_b='/cert_b='\\n/g' | awk "/-----START/,/-----END/"
This script will be executed by non-root user. It is working fine for root user.
Now I want to remove all \n
ewline characters and extract only the string inside -----START,-----END
. Hence my output should be:
-----START
abcdEFGHij
KLMN
O/PqR
-----END
Can anyone please let me know how to resolve this?
Thanks in advance
Solution
You need to first replace two characters \
n
by a newline character.
text=$(cat <<EOF
-----START\nabcdEFGHij\nKLMN\nO/PqR\n-----END\n'This is a sample text..
EOF
)
echo "$text" | sed 's/\\n/\n/g' | awk "/-----START/,/-----END/"
Note: in your script, "\n"
is just n
. If you want to preserve \
, you have to write "\\n"
or '\n'
or use a here document as above.
Answered By - KamilCuk Answer Checked By - Marie Seifert (WPSolving Admin)