Timeline for How to replace multiple occurrences of a pattern with sed
Current License: CC BY-SA 3.0
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 15, 2015 at 21:27 | comment | added | Andy | @G-Man Andy's cat...that made me laugh | |
| May 15, 2015 at 21:25 | comment | added | CaptainCap |
You can do the same thing in perl: perl -i -pe 's/\[\s+((\x27|\x22).*?$2)\s+\]/[$1]/g'
|
|
| May 15, 2015 at 21:19 | comment | added | G-Man Says 'Reinstate Monica' |
I agree that non-greedy regular expressions are more elegant than the @Captain’s answer. But that second answer (with the \2s) handles $myarray[ "Andy's cat" ] correctly, which ['"].*?['"] will not.
|
|
| May 15, 2015 at 21:12 | comment | added | CaptainCap | I agree Otheus, perl would be more elegant in that case | |
| May 15, 2015 at 21:09 | vote | accept | Andy | ||
| May 15, 2015 at 21:04 | comment | added | Otheus |
To the OP: In this case, perl -i -pe might be more useful, since perl RE's will distinguish between "greedy" and "non-greedy" globs. You want "non-greedy" in this case. The syntax would be nearly the same: perl -i -pe 's/\[\s+(\x27.*?\x27)\s+\]/[$1]/g' Or something like that.
|
|
| May 15, 2015 at 20:55 | history | edited | CaptainCap | CC BY-SA 3.0 |
added 127 characters in body
|
| May 15, 2015 at 20:50 | history | answered | CaptainCap | CC BY-SA 3.0 |