Find the answer to your Linux question:
Results 1 to 4 of 4
Hi Im a newbie to bash scripting and have been trying to do some substituting I have this string : Code: gIssueSymbol^_string^_GAR.TAR1 wPeopleSense^_int92837 gIssueSymbol^_string^_GOP.TAR1 wPeopleSense^_int92874gIssueSymbol^_string^_GOL.TAR3 wPeopleSense^_int93837gIssueSymbol^_string^_RTE.TAR1 wPeopleSense^_int91237 and I want ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    2

    bash regex to append text after a string


    Hi

    Im a newbie to bash scripting and have been trying to do some substituting

    I have this string :
    Code:
    gIssueSymbol^_string^_GAR.TAR1 wPeopleSense^_int92837 gIssueSymbol^_string^_GOP.TAR1 wPeopleSense^_int92874gIssueSymbol^_string^_GOL.TAR3 wPeopleSense^_int93837gIssueSymbol^_string^_RTE.TAR1 wPeopleSense^_int91237
    and I want to add a small s after the last u

    so its gIssueSymbol^_string^_s for every occurence in the file?

    i.e
    Code:
    gIssueSymbol^_string^_sGAR.TAR1 wPeopleSense^_int92837 gIssueSymbol^_string^_sGOP.TAR1 wPeopleSense^_int92874gIssueSymbol^_string^_sGOL.TAR3 wPeopleSense^_int93837gIssueSymbol^_string^_sRTE.TAR1 wPeopleSense^_int91237
    I've played around with sed and cant get it working - think im close though.



    I think if I try :

    Code:
    sed  s/gIssueSymbol^_string^_/s/ filename.txt >newfile.txt
    but its not working.

    Can you please help!

    Thanks


  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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:
    Code:
    sed 's/gIssueSymbol\^_string\^_/s/' filename.txt > newfile.txt
    You will also note that I've added single quotes: this is to prevent Bash from catching the backslashes instead of sed. Don't worry about it too much, but proper quoting is very important in Bash scripting.

    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.

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    2
    Thanks!

    Code:
    sed 's/gIssueSymbol\^_string\^_/gIssueSymbol\^_string\^_s/' filename.txt > newfile.txt
    just seems to rewrite the existing file to the new file

    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.

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •