0

I want to remove a string in a file, the string I want to remove is "/package/myname:". I try to use sed to do that but could not. Note there are a '/' at beginning and ':' at the end of the string which I do not know how to handle.

e.g. I was able to remove "package/myname" using:

echo 'diff a/package/myname:/src/com/abc' | sed -e 's/\<package\/myname\>//g'

But when I run:

echo 'diff a/package/myname:/src/com/abc' | sed -e 's/\<\/package\/myname\:\>//g'

the result does not replace anything.

What is the right way to remove "/package/myname:" in my case?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user389955
  • 9,605
  • 14
  • 56
  • 98

3 Answers3

2

Problem:

echo 'diff a/package/myname:/src/com/abc' | sed -e 's/\<\/package\/myname\:\>//g'
                                                       ^                    ^
                                                       |                    |     

problem is mainly because of the two word boundaries.

\< - Boundary which matches between a non-word character and a word character.

\> - Boundary which matches between a word character and a word character and a non-word character.

So in the first case,

echo 'diff a/package/myname:/src/com/abc' | sed -e 's/\<package\/myname\>//g'

The \< before the package string matches the boundary which exists after / (non-word character) and p (word character). Likewise \> matches the boundary which exists between e (word character) and : (non-word character). So finally a match would occur and the characters which are matched are replaced by the empty string.

But in the second case,

echo 'diff a/package/myname:/src/com/abc' | sed -e 's/\<\/package\/myname\:\>//g'

\< fails to match the boundary which exists between a and forward slash / because \< matches only between the non-word character (left) and a word character (right) . Likewise \> fails to match the boundary which exists between : and / forward slash because there isn't a word character in the left and non-word character at the right.

Solution:

So, i suggest you to remove the word boundaries <\ and />. Or, you could do like this,

$ echo 'diff a/package/myname:/src/com/abc' | sed -e 's/\>\/package\/myname\://g'
diff a/src/com/abc

I think now you could figure out the reason for the working of above command.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • @Abinash: perfect answer! In fact I was not clear about the word boundary /< when I asked this question. Now I realized that it is used only if I need to remove a string which is a WHOLE word in stead of partial word. http://stackoverflow.com/questions/1032023/sed-whole-word-search-and-replace. So in my case, I should not include the word boudary in sed. Thanks! – user389955 Jan 19 '15 at 19:41
1
sed 's%/package/myname:%%g'

using % instead of / to mark the ends of the sections of the substitute command. You can use any character that doesn't appear in the string. It can be quite effective to use Control-A as the delimiter, even.

You could also use:

sed 's/\/package\/myname://g'

but I prefer to avoid messing around with backslashes when there's an easy way to avoid them.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1
 sed -i "s/\/package\/myname\://g;" [__YOUR_FILE_NAME__]

That removes the phrase.

Doesn't not remove the line.

grep -v  # removes the line
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
terary
  • 940
  • 13
  • 30
  • `sed` is perfectly capable of deleting the line, of course: `sed '/\/package\/myname:/d'` does the job nicely. I'm not sure about the `\:` in your notation — did you test it? And the semicolon is superfluous but benign. – Jonathan Leffler Jan 17 '15 at 02:54
  • Yeah - I tested. sed (GNU sed) 4.2.2. Doesn't remove the line, just the test I tell it to remove. you can choose to \: or not - just trying to be helpful – terary Jan 17 '15 at 03:25
  • @Jonathan and terary: Thanks for your answer. After comparing your command with mine, I realized that I should not have included word boundary in my command, the word boundary is used only to remove a whole word, not a partical word in a string. – user389955 Jan 19 '15 at 19:44