Remove trailing characters while adding leading characters
Text file contains numerous strings with trailing sub-string.
example where _xx is the trailing sub-string; Quote:
Quote:
|
sed is your friend - use regex and capture groups. Do-able in a single invocation.
|
Please provide what you have tried so we may assist?
|
Quote:
|
Quote:
Code:
#!/bin/env python3 |
With sed:
Code:
sed 's/"\([^"]*\)_xx"/"yy_\1"/g' testText [^"] is a character that is not a " [^"]* is such a character any times. The /g modifier looks for further matches/substitutions in the line; a further match is right from the current match. |
Quote:
|
Quote:
How should this sed command be modified to work in such cases? I've tried a few things but nothing changed. |
If " anchors cannot be used, you can try \b anchors ("word boundaries"):
Code:
sed 's/\b\([^" ]*\)_xx\b/yy_\1/g' testText The pre-defined "word boundary" is just a marker not a character, so it must not be re-inserted. But it is less precise e.g. also occurs at a - character. The following uses Extended RegularExpression and three ( ) groups: Code:
sed -E 's/(^|[" ])([^" ]*)_xx([" ]|$)/\1yy_\2\3/g' testText The 2nd group is a string of not " or space characters. The 3rd group is a " or space character or the end marker. \1 \2 \3 is what the respective group has matched. |
An alternate approach is to specify what you are looking for, rather than what you are not looking for. Also protects from overlooking possible corner cases (like what if one of those blanks is a tab ?).
Code:
sed -r 's/([[:alnum:]]+)_xx/yy_\1/g' input.file |
Just to clarify: a “regular expression (regex …)” can not only match a string pattern – (“yes or no, does it match?”) – but also return to you various specified pieces of the matching string. Such as: “some other text” and “more text.” In environments like sed, these pieces are instantly available as things like [left to right …] “$1” and “$2.” Or maybe, “\1” and “\2.” Which you can immediately use to produce output.
Also: These days, “regex support” is universal, and the syntax has become standardized. Implementations now vary only in the details. Every language has it. Therefore, understanding this very important power-tool is definitely “an essential life skill.” (Like knowing how to use a life jacket …) If you need to “tear apart a text string,” (and who doesn’t?), regex has your back. There are “esoteric fee-churs” in regexes that you can learn about if and when you actually need them, and others that you might use every day. |
Quote:
|
Quote:
Thanks! |
You need to take that "deep dive" - regex is a powerful and useful tool. MadeInGermany has given you good pointers to get you started.
|
Please forgive if this is obvious to LQ regulars.
The excellent solution posted by syg00 may be generalized. xx and yy could be variable names instead of character strings. With this InFile ... Code:
m1_SALT some other text m2_SALT Code:
xx='SALT' Code:
SUGAR_m1 some other text SUGAR_m2 but not HAM into CHEESE. Daniel B. Martin . |
All times are GMT -5. The time now is 11:44 PM. |