Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

TEXT=abcdef-lalala-someText-yahoo

After the sed execution, that line should look like:

TEXT=abcdeflalalasomeTextyahoo

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

2 Answers 2

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'
TEXT=abcdeflalalasomeTextyahoo
share|improve this answer
1  
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

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.