Skip to main content

All Questions

Tagged with
32 votes
1 answer
9k views

How to ensure that string interpolated into `sed` substitution escapes all metachars

I have a script that reads a text stream and generates a file of sed commands that is later run with sed -f. The generated sed commands are like: s/cid:image002\.gif@01CC3D46\.926E77E0/https:\/\/...
dan's user avatar
  • 4,097
415 votes
4 answers
774k views

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
detly's user avatar
  • 5,220
19 votes
2 answers
94k views

Passing a variable to sed [duplicate]

I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script: cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt The result in shared2.txt looks ...
user2013619's user avatar
13 votes
2 answers
5k views

Why do I need to escape regex characters in sed to be interpreted as regex characters?

It seems e.g. cat sed_data.txt | sed 's/\b[0-9]\{3\}\b/NUMBER/g' that I must escape characters to form a regular expression. In this case I had to escape braces in order to be interpreted as a number ...
Jim's user avatar
  • 10.2k
2 votes
3 answers
11k views

Using a reference to a bash string variable in sed

I'm a relative Linux novice. Suppose that I have a text file a.txt that contains the following text: A B C Now, if I want to change the text on line 2 (which contains B), I can use the following ...
Andrew's user avatar
  • 17.1k
7 votes
1 answer
598 views

Variable in sed

I have a file that contains filenames with absolute path. In the middle of that path, is a variable directory, whenever I hard code the variable part, it works fine, but when I replace it with the ...
bostjan's user avatar
  • 73
5 votes
2 answers
20k views

How to escape a single quote? [duplicate]

This works fine: sed -i 's# @driver.find_element(:xpath, "//a\[contains(@href,##' temp_spec.rb against a source of @driver.find_element(:xpath, "//a[contains(@href,'change_district')]").click I ...
Michael Durrant's user avatar
6 votes
1 answer
37k views

How to use variables in sed command

I'm trying to run sed -i "/$variable/c \$variable1' file.txt But it's not displaying any change in the file: file.txt. But the same command, with out the variables works fine: sed -i "/variable/c \...
John's user avatar
  • 63
4 votes
5 answers
30k views

How to use a shell variable inside sed's s command? [duplicate]

we know that we can get the value of a variable using $ sign: x=3 echo $x 3 Is there other ways that we can use to get the value without using $ sign. I'm asking this because $ sign is a special ...
Nidal's user avatar
  • 9,006
4 votes
2 answers
20k views

Escaping ' (single quote) in sed replace string [duplicate]

I have a script that I'm using to perform some iptables changes across servers. In the script I have a sed command to find a string and insert iptables rule before that line. The problem I'm having ...
Double's user avatar
  • 53
3 votes
4 answers
2k views

Piping paths with different types of quotes for slash substitution

I would like to use sed to convert a path with backslashes to the same path with forward slashes: E.g. I would like to pipe \\path\to\file\ and obtain /path/to/file None of the following commands ...
Amelio Vazquez-Reina's user avatar
81 votes
2 answers
266k views

Slash and backslash in sed [duplicate]

I want to use sed to change a slash into a backslash and a slash, i.e. / -> \/. But it does not work. Here a small example: #!/bin/bash TEST=/etc/hallo echo $TEST echo $TEST | sed "s/hallo/bello/g" ...
devopsfun's user avatar
  • 1,417
45 votes
1 answer
52k views

using sed with ampersand (&)

I'm using sed to find and replace patterns within SAS files I have rather than changing them individually. The problem is I am trying to replace macro variables and when I use the ampersand it is not ...
DukeLuke's user avatar
  • 553
35 votes
4 answers
72k views

find and replace with sed with slash in find and replace string

I want to change in file /var/www with /home/lokesh/www with sed command sed -i 's///var//www///home//lokesh//www/g' lks.php but this give error sed: couldn't open file ww///home//lokesh//www/g: No ...
LOKESH's user avatar
  • 505
5 votes
2 answers
8k views

Escaping blackslash in sed

I am trying to write a sed command to replace a line in a file. The sed replace required the current working directory, which starts making the command a bit messy because of the characters that need ...
Franz Payer's user avatar

15 30 50 per page