Getting bad flag in substitute command '/' for sed replacement

DisplayName

I am facing one issue while using replace in my sed command. I've a file named a.txt, I want to replace a single line with some other line but I am getting above mentioned error.

Code I want to replace:

DOMAIN_LOCAL = "http://127.0.0.1:3000";

I want it to replace with any other ip lets say something like below:-

DOMAIN_LOCAL = "http://127.1.1.2:3000";

What I've tried:-

ip=http://127.1.1.2:3000
sed "s/DOMAIN_LOCAL = .*$/DOMAIN_LOCAL = "$ip";/g" a.txt

But it is giving me following error. Can somebody help here.

sed: 1: "s/DOMAIN_LOCAL = .*$/DO ...": bad flag in substitute command: '/'
andlrc

You will need to use another delimiter. And escape the $ or use single quotes:

% ip="http://127.1.1.2:3000"
% sed 's~DOMAIN_LOCAL = .*$~DOMAIN_LOCAL = "'"$ip"'";~' a.txt
DOMAIN_LOCAL = http://127.1.1.2:3000;

When you use / as a delimiter in the substitute command it will be terminated at the first slash in http://:

sed 's/DOMAIN_LOCAL = .*$/DOMAIN_LOCAL = http://127.1.1.2:3000;/'
#                                             ^ here

Breakdown:

sed 's~DOMAIN_LOCAL = .*$~DOMAIN_LOCAL = "'"$ip"'";~' a.txt
#   │                                    ││└ Use double quotes to avoid word splitting and
#   │                                    ││  globbing on $ip
#   │                                    │└ Exit single quotes
#   │                                    └ Literal double quotes
#   └ Using single quotes to avoid having to escape special characters

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sed command: bad flag in substitute command: '-'

Sed error: bad flag in substitute command: 'U'

bash: bad flag in substitute command with escaped slashes

sed throws 'bad flag in substitute command' but works fine in package.json

"bad flag in substitute command: '{" while replacing a string with sed from one file to another file

Sed command failing with bad flag in script

Sed substitute pattern with command

sed select line and substitute string in one command

Use output of one command as replacement text in sed

Meaning of ",$d" in replacement part of sed command

Replacement for double asterisk in `sed` command file path

Linux sed -i -e command include / in replacement

Use bash sed command for double backslash replacement

sed command works with e flag but not with E

Substitute: replacement evaluation

How to use substitute command in unix(vi or sed) to achieve the following results

why the ending g keyword in this sed substitute command is not matching all the occurrences?

a command substitute

Using sed with command-substitution text in the replacement part is not working

sed with replacement part given as input from command line

Shell execute grep command within sed replacement not as expected

Github Action - How to keep the quotes in the sed command replacement?

Substitute with backslash in sed

sed substitute with regex

Substitute a string containing && with sed

Is sed allowed to create a file if w command or flag is never executed?

sed s command & e flag: `echo '$-' | sed 's/-//e'` gives: `sh: $: command not found`

Getting unterminated 's' command with sed (bash)

getting a part of the output from a sed command

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive