Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Oct 2010
bash regex to append text after a string
Im a newbie to bash scripting and have been trying to do some substituting
I have this string :
gIssueSymbol^_string^_GAR.TAR1 wPeopleSense^_int92837 gIssueSymbol^_string^_GOP.TAR1 wPeopleSense^_int92874gIssueSymbol^_string^_GOL.TAR3 wPeopleSense^_int93837gIssueSymbol^_string^_RTE.TAR1 wPeopleSense^_int91237
so its gIssueSymbol^_string^_s for every occurence in the file?
gIssueSymbol^_string^_sGAR.TAR1 wPeopleSense^_int92837 gIssueSymbol^_string^_sGOP.TAR1 wPeopleSense^_int92874gIssueSymbol^_string^_sGOL.TAR3 wPeopleSense^_int93837gIssueSymbol^_string^_sRTE.TAR1 wPeopleSense^_int91237
I think if I try :
sed s/gIssueSymbol^_string^_/s/ filename.txt >newfile.txt
Can you please help!
Your command is close. However, there are a few problems:
First of all, '^' is a special character in regular expressions that means "at the beginning of the string". If you want to use a literal '^' in your regular expression, you have to "escape" it:
sed 's/gIssueSymbol\^_string\^_/s/' filename.txt > newfile.txt
Alrighty then. Now, what this command is actually saying is "Replace the first occurrence of the substring 'gIssuesSymbol^_string^_' with 's'". This is probably not what you want.
The first thing you want to fix is that you don't want to replace the matched text: you want to append to it. I suggest that you read about "matches" in sed and how to use backreferences in your replacement text.
The second problem is that you want to replace the last occurrence, not the first one. This is a harder problem to fix, but I would do it by using greedy quantifiers. You may want to read about them.
I don't want to give you the answer directly, but please ask if you don't understand any of what I've said.
- Join Date
- Oct 2010
sed 's/gIssueSymbol\^_string\^_/gIssueSymbol\^_string\^_s/' filename.txt > newfile.txt
In my file ^_ dont seem to be meta chars , ^isnt a caret - to get caret ^ for example you need to press control then v then underscore. Maybe this is messing it up?
Last edited by HUNGRYSUMO; 10-08-2010 at 08:01 AM.
Ah, yes, if that's not a literal "^_", then this regex won't work. On the other hand, as far as I know, Ctrl-v + _ should be a literal underscore, so that's some weirdness right there.
You will need to figure out what the "^_" in your files means, and how to replicate that character in the sed command.
Beyond that, your regex will still only match the first occurrence, not the last one.