Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How exactly does sed view meta characters when you replace or delete text?

null_reflections

Legendary Coder
I'm trying to figure out how to use sed without erasing extra things that i want to keep in the document. So far, i've realized that sed is largely user-friendly in terms of how it does away with strings and numbers:

Code:
sed -i '/cow/d' <file>

sed -i '/10/d' <file>

in both of these examples, sed will only get rid of entire instances of "10" and "cow", and will not erase every 1,0,c,o,w. However, it doesn't work this way with metacharacters. Let's say i had "**" in a document that i wanted to erase, how do i do that without getting rid of every asterisk? For some reason, it erases all the test in the document when i do
Code:
sed -i '/**/d' <file>
, is it just reverting to the bash wildcard? Why does it do that?

Just to be clear, this works just fine if i wanted to get rid of all the asterisks for some reason:
Code:
sed -i '/*/d' <file>
 
Last edited:
Solution
I've answered part of this: you can edit separate strings in sed like this, and it works if you have two ** in a file but only want to get rid of that particular string:

Code:
sed -i '/\*\*/d' <file>

This will get rid of every instance where there are two asterisks next to each other, but not instances where there's a lone asterisk. Sed tends to find exact matches unless you use meta characters in regular expressions to broaden things

However, i still wonder why sed erases all the text in my file when i have the two asterisks next to each other.
I've answered part of this: you can edit separate strings in sed like this, and it works if you have two ** in a file but only want to get rid of that particular string:

Code:
sed -i '/\*\*/d' <file>

This will get rid of every instance where there are two asterisks next to each other, but not instances where there's a lone asterisk. Sed tends to find exact matches unless you use meta characters in regular expressions to broaden things

However, i still wonder why sed erases all the text in my file when i have the two asterisks next to each other.
 
Last edited:
Solution

Buy us a coffee!

Back
Top Bottom