Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to use the sed command to get rid of some dashes in a string (inside a file), i.e., the file has a line that says:


After the sed execution, that line should look like:


Thanks in advance

share|improve this question
-1 for not properly describing what you were looking for. – barlop Mar 12 '12 at 23:45
Wilmer has quite reasonably phrased his question. Taken literally and considering a programmer's idiom, it is pretty clear what he wants. He speaks of "a" string and "that" line, but does not mention any or all lines. Typically "foo", "abcdef", "lalala" mean variable data; therefore I can assume that "TEXT=" and "yahoo" are constants (unless he says otherwise). I was originally ambivalent about "yahoo", but since the mark-downs, I've re-focused on the question and I should adjust my answer, even though Wilmer was gracious enough to not complain about my oversight. – Peter.O Mar 13 '12 at 2:59
up vote 1 down vote accepted

Based on your further comment to Nifle's answer, perhaps this is more like what you are looking for. It will only acts on lines which contain TEXT= followed by 4 '-' delimited text segments.

Using GNU sed (treating "TEXT" and "yahoo" as constant)

sed -re 's/^(TEXT=[^-]+)-([^-]+)-([^-]+)-(yahoo)$/\1\2\3\4/'

Or, using GNU sed (treating all text after "=" as vairable)

sed -re 's/^(TEXT=[^-]+)-([^-]+)-([^-]+)-([^-]+)$/\1\2\3\4/'

Or, for posix portability (treating all text after "=" as vairable):

sed 's/^\(TEXT=[^-][^-]*\)-\([^-][^-]*\)-\([^-][^-]*\)-\([^-][^-]*\)$/\1\2\3\4/'
share|improve this answer

Here you go:

echo "TEXT=abcdef-lalala-someText-yahoo" | sed 's/-//g'
share|improve this answer
sed -i 's/-//g' filename is what would actually change it in the file, it would remove EVERY - in the file though, which might not be what you want. – Rob Mar 11 '12 at 18:52
Even though that works, I was thinking in little more sophisticated way to di it, for instance: sed -i 's/TEXT=......-......-........-...../wayToTellSedToLeaveOriginal6CharsWayToTellS‌​edToLeaveOriginalSecond6CharsAndSoOn"7g' filename – Wilmer Mar 12 '12 at 0:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .