LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using wildcard with the sed s/// command (https://www.linuxquestions.org/questions/linux-newbie-8/using-wildcard-with-the-sed-s-command-4175470096/)

IneptCoder 07-18-2013 01:11 PM

using wildcard with the sed s/// command
 
I am trying to substitute all the words ending in a "ing" with nothing (aka removing "ing") so essentially I tried this:

sed s/" *ing "// ./myfile

but it doesn't work. I have also tried using wildcard with sed several other times but it doesn't seem to do what it is supposed to do. I am thinking that I need to quote the wildcard somehow, but I have tried many different ways. Does the sed substitute command support using wildcard? if it does how do I use it?

Firerat 07-18-2013 01:57 PM

the wildcard in sed is a strange beast

it is actually looking for " " ( spaces ) and only " " no matter how many

Code:

sed 's/ .*ing//' ./myfile
will do what you want ( well maybe not )
here . is 'wild' for any character
BUT, that will wipe out everything up to the last ing

to avoid this you could limit to just alphas ( so space is not part of the pattern )

Code:

sed 's/[a-Z]*ing//' ./myfile
That will only operate on the first match of each line, to get all matches


Code:

sed 's/[a-Z]*ing//g' ./myfile
Note the g on the end

grail 07-18-2013 05:21 PM

Well I have read the question a few times and the part that catches my eye is:
Quote:

aka removing "ing"
If we are removing "ing" from all words then the following would suffice:
Code:

sed 's/ing//g' file
Of course the downside here is that you may have a word like "working" so when you remove string you get "work", but it also means that a word like "something"
will become "someth" which is not a word

David the H. 07-19-2013 07:29 AM

The real answer is that, no, there are no "wildcards" in sed. Tools like sed and grep use regular expressions, which is a different, more complex, and more powerful beast altogether.

It's well worth the effort to learn at least the basics of regex, as one or another flavor of it is used in most text processing tools and programming languages. Even your bash shell supports it (inside the [[..]] test construct).

Here are a few regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/


As for what you call "wildcards", the *nix version is usually called globbing. Globbing is a much less sophisticated descendant of regex used for matching simple patterns in filenames and strings. Your shell uses it in several ways, particularly for matching multiple files at once, and some other tools like find also support them.

These pages detail the globbing types used in bash. A google search or two with terms like "globbing tutorial" should turn up more info.

globbing
extended globbing


Here are a few useful sed references for you too:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.catonmat.net/series/sed-one-liners-explained

IneptCoder 07-19-2013 09:39 AM

Thanks for the help


All times are GMT -5. The time now is 09:32 PM.