Search code examples
regexfish

How do I include a comma in a Regex expression?


I started using fish today, and am using sed to search for:

}\n

I want to replace it with:

},\n

I tried

`sed -i -- 's/}\n/},\n/g' EU_Users.json`

but when I checked the file there were no commas present.

How can I add them in?


Solution

  • The problem is related to the fact that sed does not add \n to the pattern space by default, it uses newlines to split a stream into lines.

    You may use

    sed -i -- 's/}$/},/' EU_Users.json
    

    The }$ will match a } at the end of the line ($) and these matches will be replaced with },.

    Note that g modifier is not necessary: sed will process line after line, and since there is only one end of a line in each line, we only need a single search and replace operation.