skip to Main Content

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


  1. 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.

    sed -i '/"'"$SUBSTR"'"[.]"/{ s/"//g; s/'"$SUBSTR"'[.]/ /; }' $1
    

    This scans for lines matching the desired pattern, ignoring all others.
    ON A MATCH, it

    • eliminates all double-quotes from the file with the first s///
    • then closes the singles so the second double-quotes can properly quote your variable in case there is embedded spaces or such,
    • then closes the double quoting after the variable,
    • opens single quotes again,
    • bracket-quotes the literal dot you want to match,
    • then ends the search pattern and replaces it with the space.

    Original (wrong) answer


    If I’m reading it right, you’re doing a lot of work to accomplish this:

    sed -Ei 's/[^ ]+[.]//' WSH.txt
    

    The problem preventing it from working is the double quotes that aren’t being matched. You included them as part of the pattern.

    sed -i 's/"$SUBSTR"."/ /g' $1 # sub "WSH"<anychar>", not what you want.
           ^  ^       ^ ^    ^
    

    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:

    sed -i "s/$SUBSTR[.]/ /g" $1 # double quotes around the whole pattern
    

    or

    sed -i 's/'"$SUBSTR"'[.]/ /g' $1 # switching quotes 
    

    or even, for this case,

    sed -i s/"$SUBSTR"'[.]/ /g' $1 # only quote what needs it 
    

    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 for sed – they are so the command parser gets the string right before passing it to sed, 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.

    Login or Signup to reply.
  2. You can do the replacement in one command:

    sed 's/"WSH"."/ /g' WSH.txt
    

    output:(SPACE)WSH_INTERFACE_ERRORS"
    Isn’t that what you need?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search