Issue
I would like to replace :
S-digit-digit-digit-digit-digit-digit-digit
by
S_digit_digit_digit_digit_digit_digit_digit
Here's the exemple :
S-1-5-21-1119404543-3273515248-2536353511-2784
S_1_5_21_1119404543_3273515248_2536353511_2784
I tried this :
sed -e 's/[[.]]-[[:digit:]]-[[:digit:]]-[[:digit:]]-[[:digit:]]-[[:digit:]]-[[:digit:]]-[[:digit:]]/s/[[.]]_[[:digit:]]_[[:digit:]]_[[:digit:]]_[[:digit:]]_[[:digit:]]_[[:digit:]]_[[:digit:]]/g'
Solution
[[:digit:]]
matches a single digit.[:digit:]
is a character class. sed manual character classes[[.]]
is just invalid. There is no[.]
"character class" and[
if inside[...]
has to be the last character and]
inside[...]
has to be the first. If you meant to match a single any character, just do a dot.
. I recomment regex crosswords to learn regular expressions.- I believe you meant to just match the expression, save the numbers with backreferences and replace the dashes. Here is a great sed tutorial.
So:
$ sed 's/\(.\)-\([[:digit:]]*\)-\([[:digit:]]*\)-\([[:digit:]]*\)-\([[:digit:]]*\)-\([[:digit:]]*\)-\([[:digit:]]*\)-\([[:digit:]]*\)/\1_\2_\3_\4_\5_\6_\7/g' <<<'S-1-5-21-1119404543-3273515248-2536353511-2784'
S_1_5_21_1119404543_3273515248_2536353511
You can also hold the line, extract the relevant part that you want all dashes replaced with underscore by using some separator, then replace all the dashes and grab the line and shuffle with the original input for the output:
$ sed 's/.\(-[^-]*\)\{7\}/\n&\n/;h;s/-/_/g;G;s/.*\n\(.*\)\n.*\n\(.*\)\n.*\n/\2\1/' <<<'unrelated text S-1-5-21-1119404543-3273515248-2536353511-2784 unrelated text'
unrelated text S_1_5_21_1119404543_3273515248_2536353511_2784 unrelated text
Answered By - KamilCuk Answer Checked By - Terry (WPSolving Volunteer)