Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using "sed" to find and delete a phrase in a text file. It works great, but I can't seem to make it get specific. For example, this is my "sed" command:

sed -i 's/foobox.com//' ~/Foo/fooman.txt

I am looking for 6 specific phrases in ~/Foo/fooman.txt

they are:

dl-web.foobox.com
dl.foobox.com
foobox.com
fooboxdownloads.com
www.foobox.com
www.fooboxdownloads.com

When I run sed -i 's/foobox.com//' ~/Foo/fooman.txt it finds every enty with "foobox.com" and deletes it, but leaves the left overs there (i.e. "dl-web." "dl." "downloads.com" "www." "www.downloads.com".

Can I make "sed" use a "wildcard" when searching for the phrase? If so, where does one put the wildcard (I have already tried (*) which does not work). If not using a wildcard, can "sed" use some other method to get specific (dl-web.foobox.com) instead of just being general (foobox.com)? id "sed" can't do the trick, is there a nother CI command that would do the trick?

Thanks for any help, I am new to Linux and writing CI commands.

share|improve this question
add comment

3 Answers

it depends how the strings are delimited

Say thy are delimited by spaces then something like this may do the trick:

sed 's/ .*foobox.*\.com / /g'

The 'g' will make sure that if there is more than one match in a line all matches will be substituted

tested with

DEF  dl-web.foobox.com XYZ
DEF dl.foobox.com XYZ
DEF  foobox.com XYZ
DEF  fooboxdownloads.com XYZ
DEF  www.foobox.com XYZ
DEF  www.fooboxdownloads.com XYZ 

producing

DEF XYZ
DEF XYZ
DEF XYZ
DEF XYZ
DEF XYZ
DEF XYZ 
share|improve this answer
add comment

[[:alnum:].-] will match an alphabetic or numeric character, or a . or a -. Put a * after it and it will match zero or more of that (greedily). That should probably cover the possible characters in a hostname. Also note that . matches any character, so if you want to only match a ., you need to escape it with a \, or enclose it in [ and ] (that is: \. or [.]). So:

sed -i 's/[[:alnum:].-]*foobox\.com[[:alnum:].-]*//g' file

might be what you want. Note that the non-standard -i option of GNU and BSD sed does not edit files, it replaces the files. It may not matter in your case, just be aware that if any of the files are links, those links will be broken. If it does matter, consider using a command-based file editor, like ed or ex.

See http://xrl.us/sedintro#uh-0 for a nice intro to sed.

share|improve this answer
add comment

Could you use grep instead? The command

cat ~/Foo/fooman.txt | grep -v foobox.com

will remove all lines containing the phrase foobox.com. Is this what you wanted to do?

share|improve this answer
    
No, I don't want to remove the entire line, only foobox.com and any other phrase that included foobox.com (i.e. dl-web.foobox.com) –  user18432 May 21 '12 at 17:03
    
Ah, sorry, I misunderstood the question originally. Try using .* as the wildcard rather than * and see if that works. –  theabro May 22 '12 at 9:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.