I am using sed to replace all lines in the txt file matching the selected patterns in each line #make 2 substitutions with sed sed 's|\/Volumes/Macintosh HD 2/Analys/output/CNE/RG/||; s|\.log||'  where I need to replace the path &quot;/Volumes/Macintosh HD 2/Analys/output/CNE/RG/&quot; However, if I define the path into variable set in bash script path_to_replace='/Volumes/Macintosh HD 2/Analys/output/CNE/RG/'  and then to use it with sed sed 's|\&quot;${path_to_replace}&quot;||; s|\.log||'  sed do nothing. How the variable could be properly used with the sed function?
4793
I want to do:  cat update_via_sed.sh | sed 's/old_name/new_name/' &gt; new_update_via_sed.sh   in my program.  But I want to use variables, e.g.  old_run='old_name_952' new_run='old_name_953'   I have tried using them but the substitution doesn't happen (no error). I have tried:  cat update_via_sed.sh | sed 's/old_run/new_run/' cat update_via_sed.sh | sed 's/$old_run/$new_run/' cat update_via_sed.sh | sed 's/${old_run}/${new_run}/'
11519
I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep -m 1 &quot;Screen Shot&quot;; echo &quot;Most recent screenshot is: &quot;$thefile;  But that outputs: Screen Shot 2011-07-03 at 1.55.43 PM.png Most recent screenshot is:   So, it looks like that isn't getting assigned to $thefile, and is being printed as it's executed. What am I missing?
104860
I am trying to delete two lines ( 7 and 8) from a .txt file. For this I am using following code:-  #!/bin/sh Column="7" sed '"$Column",8d' myfile.txt &gt; result.txt   on running this script, I am getting this error:-  sed: -e expression #1, char 1: unknown command: `"'   Please tell me how to use variable as a part of sed command.
51106
Let M=3 and N=4. I want to replace line M in file1 with line N in file2. I can replace with a string using sed:  sed -i '3s/.*/stringToReplace/' file1   And I can use awk to get line N from file2  awk 'NR==4' "file2"   How can I combine these two? If I try  sed -i '3s/.*/{awk 'NR==4' "file2"}/' file1   then I replace line M with literally {awk 'NR==4' "file2"}.
91376
Is this possible? I read somewhere that the following command would do it:     sed -e [command] [file]   but it appeared to do the same thing as just     sed [command] [file]   (it did not save the changes). Is there any way to do this using sed?
110012
the text in file looks like this:  [homes]         comment = Home Directories         path =          browseable =          writable = yes         valid users = %S         valid users = MYDOMAIN\%S  [printers]         comment = All Printers         path = /var/spool/samba         browseable = no         guest ok = no         writable = no         printable = yes   i want output as:  [homes]         comment = Home Directories         path = /data         browseable = yes         writable = yes         valid users = %S         valid users = MYDOMAIN\%S  [printers]         comment = All Printers         path = /var/spool/samba         browseable = no         guest ok = no         writable = no         printable = yes   i am using this command:  sed -i "\#path# s#.*#&amp; /data#" file   but it makes changes to everywhere in file where path is located  can anyone help me with this?
71671
My script is:  n="y" while [ "{n}" = "y" ] if [ $n == "n" ]; then   break; fi echo "n is $n" do         read -p "Enter the word to find = " word         read -p "Enter word to replace = " replace         echo "$word n $replace"         #sed -i r.text.bak 's/$word/$replace/g' r.txt         sed -i "s/$word/$replace/g" "test.txt" echo "do you have further replacement? n or y" read temp n=$temp done   My problem is that I am also replacing partial matches. For example, for a line like this:  1.1.1.14 1.1.1.14567   I get this output:  1.1.1.3  1.1.1.3567   but I expected:  1.1.1.3 1.1.1.14567   How can I solve this?
55260
If I run these commands from a script:  #my.sh PWD=bla sed 's/xxx/'$PWD'/' ... $ ./my.sh xxx bla   it is fine.  But, if I run:  #my.sh sed 's/xxx/'$PWD'/' ... $ ./my.sh $ sed: -e expression #1, char 8: Unknown option to `s'    I read in tutorials that to substitute environment variables from shell you need to stop, and 'out quote' the $varname part so that it is not substituted directly, which is what I did, and which works only if the variable is defined immediately before.  How can I get sed to recognize a $var as an environment variable as it is defined in the shell?
34120
I am looking for a way to replace a string in a file with a string that contains a slash by using sed.  connect="192.168.100.61/foo" srcText="foo.bar=XPLACEHOLDERX" echo $srcText | sed "s/XPLACEHOLDERX/$connect"   The result is:  sed: -e Expression #1, Character 32: Unknown option for `s'
19480
I tried the following code to replace QQ with ZZ, but it doesn't do what I want:  var1=QQ sed -i 's/$var1/ZZ/g' $file   However, this code does what I want:  sed -i 's/QQ/ZZ/g' $file   How do I use variables in sed?
87408
Oh, hi; So I've got this big json file and I'm trying to find and replace a few things but when I've used sed I get an error and I need a sed or awk string to find and replace it, without getting an error.  prajwal  Current: {&quot;telephone_numbers&quot;:[&quot;1-  Future: {&quot;telephone_numbers&quot;:[&quot;+1-  This is the command i tried and the error i got. sed -i 's/{&quot;telephone_numbers&quot;:[&quot;1-/{&quot;telephone_numbers&quot;:[&quot;+1-/g' output.json sed: -e expression #1, char 56: unterminated `s' command
19044
I execute a sed command in my script:  sed -i "${1}s/${2}=.*/${2}=${REPLY}/" $3   this is the echo:  sed -i 13s/log4j.appender.ROOT2.File=.*/log4j.appender.ROOT2.File=/app/TET/log.log/ /applis/DIAPASON/diapason1/conf/log4j.properties   My error:  sed: -e expression n°1, character 61: unknown option fors`'
5286
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:\/\/mysite.com\/files\/1922/g s/cid:image003\.gif@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1923/g s/cid:image004\.jpg@01CC3D46\.926E77E0/https:\/\/mysite.com\/files\/1924/g   Assume the script which generates the sed commands is something like:  while read cid fileid do     cidpat="$(echo $cid | sed -e s/\\./\\\\./g)"     echo 's/'"$cidpat"'/https:\/\/mysite.com\/files\/'"$fileid"'/g' &gt;&gt; sedscr done   How can I improve the script to ensure all regex metacharacters in the cid string are escaped and interpolated properly?
65627
It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects.  The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' command. The executed command prints found filename, test specimens, and modified filename. The 'sed' command itself is used to replace 'txt' to 'TXT'.  touch sample.txt find ./ -maxdepth 1 -name "*.txt" -exec echo {} $(echo Specimen_before.txt {} Specimen_after.txt |sed -e "s/txt/TXT/g") \;   The expected output is:     ./sample.txt Specimen_before.TXT ./sample.TXT Specimen_after.TXT   Instead it produces:     ./sample.txt Specimen_before.TXT ./sample.txt Specimen_after.TXT   (the example has been tested also with old-school command substitution through backquotes '`' with the same result)  What am I doing wrong?
54821
I know this question has probably been answered before.  I have seen many threads about this in various places, but the answers are usually hard to extract for me.  I am looking for help with an example usage of the 'sed' command.  Say I wanted to act upon the file "hello.txt" (in same directory as prompt).  Anywhere it contained the phrase "few", it should be changed to "asd".  What would the command look like?
40363
I'm trying to replace each , in the current file by a new line:  :%s/,/\n/g    But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.  What should I do?  If you are curious, like me, check the question  as well.
104690
I want to sed using a variable.  For example, sed -n '1,10p' file. So, instead of 10, I want to use $k where k is an integer.