I have one file named WSH.txt
in which I am having content –
"WSH"."WSH_INTERFACE_ERRORS"
So here what exactly I need is that – "WSH"."
should get replaced by blank space.
So now I have created one script named sed.sh
in which I have given –
SUBSTR=$(echo $1 | cut -d'.' -f 1)
sed -i 's/"$SUBSTR"."/ /g' $1
But when I am trying to run ./sed.sh WSH.txt
, I am getting output –
# cat WSH.txt
"WSH"."WSH_INTERFACE_ERRORS"
(Sorry I was unable to add image here)
But the output which I want is –
WSH_INTERFACE_ERRORS
I also tried with –
sed -i 's/"${SUBSTR}"."/ /g' $1
But still it’s not giving the result.
can someone please help me with this ?!
Thanks in advance!
2
Answers
EDIT
I read that entirely wrong.
Assuming quotes are IN the file itself…
The variable was inside single quotes and so not being parsed.
Make sure you get the quote at the end, too.
This scans for lines matching the desired pattern, ignoring all others.
ON A MATCH, it
Original (wrong) answer
If I’m reading it right, you’re doing a lot of work to accomplish this:
The problem preventing it from working is the double quotes that aren’t being matched. You included them as part of the pattern.
The doubles are inside singles, so they got passed to sed as literal characters.
I think the line you are looking for is one of these:
or
or even, for this case,
In either case, what you are actually doing is building the pattern as a string on the command line, then passing it to
sed
. The quotes aren’t forsed
– they are so the command parser gets the string right before passing it tosed
, which should never see them unless they are intended to be included as part of the pattern.https://www.gnu.org/software/sed/manual/sed.html#BRE-syntax is
sed
BRE syntax.https://www.gnu.org/software/sed/manual/sed.html#ERE-syntax is
sed
ERE syntax.https://www.gnu.org/software/bash/manual/html_node/Quoting.html is
bash
Quoting.You can do the replacement in one command:
output:(SPACE)WSH_INTERFACE_ERRORS"
Isn’t that what you need?