echo "hi,my,name,is" | sed 's/,/\n/g' &gt;out.txt   yields:  hi my name is   But in reverse:  sed 's/\n/,/g' out.txt   yields:  hi my name is   Why doesn't sed revert the operation with an inversion of the replace operation?
44642
Is there an issue with sed and new line character? I have a file test.txt with the following contents    aaaaa   bbbbb   ccccc   ddddd     The following does not work: sed -r -i 's/\n/,/g' test.txt    I know that I can use tr for this but my question is why it seems not possible with sed.     If this is a side effect of processing the file line by line I would be interested in why this happens. I think grep removes new lines. Does sed do the same?
45974
When using sed to replace strings in-place, is there a way to make it report the changes it does (without relying on a diff of old and new files)?  For instance, how can I change the command line  find . -type f | xargs sed -i 's/abc/def/g'   so I can see the changes that are made on the fly?
25014
How can I recursively replace a string in all folders and files' name with a different string? I am running Red Hat 6 and I can find them with:  find . -name \*string\*   I've managed to do it for strings within files:  find . -type f -exec sed -i 's/string1/string2/g' {} +   but how could I replace in a similar way all file names?
31972
Suppose I have a file that contains multiple occurrences of both StringA and StringB. I want to replace all occurrences of StringA with StringB, and (simultaneously) all occurrences of StringB with StringA.  Right now, I'm doing something like  cat file.txt | sed 's/StringB/StringC/g' | sed 's/StringA/StringB/g' | sed 's/StringC/StringA/g'   The problem with this approach is that it assumes StringC doesn't occur in the file. While this isn't an issue in practice, this solution still feels dirty -- that is, it feels like an opportunity to learn more unix magic. :)
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?
51572
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/' &gt; shared2.txt   The result in shared2.txt looks like:  ...$2....   What did I do wrong?
123368
I know people use : instead of / when they are dealing with paths, but  what does :g do in the following sed action?  Why is path1 between quotation mark? Can anyone explain the action of this code?  sed -i -e s:INPUT_REPLACE:"${path1}":g ${path2}
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?
14684
I am trying to perform a mathematical operation with sed, but it continues to treat my variables as strings. The input is of this kind:  $ echo 12 | sed 's/[0-9]*/&amp;+3/' $ 12+3   I'd like to have 15 as output. I need to do the operation and replace its mathematical result in only one passage, because I am running the program as a Python daemon, and I want to avoid passages like redirecting stdout on files, open those files, perform operations, extract the result, do the replacement. To me, sed seems the best to perform all in one line.  I've tried to cast both input and output in various ways like  $ echo 12 | sed 's/[0-9]*/int(&amp;+3)/' $ echo 12 | sed 's/[0-9]*/\int(&amp;+3)/' $ echo 12 | sed 's/[0-9]*/\int(&amp;+3)/'   but the result was always a printing of the second field.
92389
I have the following code that will remove lines with the pattern banana and 2 lines after it:  sed '/banana/I,+2 d' file   So far, so good! But I need it to remove 2 lines before banana, but I can't get it with a “minus sign” or whatever (similar to what grep -v -B2 banana file should do but doesn't):  teresaejunior@localhost ~ &gt; LC_ALL=C sed '-2,/banana/I d' file sed: invalid option -- '2' teresaejunior@localhost ~ &gt; LC_ALL=C sed '/banana/I,-2 d' file sed: -e expression #1, char 16: unexpected `,' teresaejunior@localhost ~ &gt; LC_ALL=C sed '/banana/I,2- d' file sed: -e expression #1, char 17: unknown command: `-'
60965
I'm looking for a way to only execute replacement when the last character is a newline, using sed.  For instance:  lettersAtEndOfLine   is replaced, but this is not:  lettersWithCharacterAfter&amp;   Since sed does not work well with newlines, it is not as simple as  $ sed -E "s/[a-zA-Z]*\n/replace/" file.txt   How can this be accomplished?
98042
I'm reading a lot of documentation on sed, and am still stumped on my particular use case.  I want to replace this line in a conf file with my own line:  Replace this line: #maxmemory &lt;bytes&gt; with: maxmemory 26gb  This is what I tried:  sed s/maxmemory.*bytes.*/maxmemory 26gb/ /etc/redis/redis.conf  I get the error: sed: -e expression #1, char 30: unterminated `s' command  Which stumps me because I don't know what that means. So my question is:  How can I accomplish what I want? What does that error mean? (so I can learn from it)
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'
92184
This  inserts a tag to the beginning of a file:  sed -i "1s/^/&lt;?php /" file   How can I insert something to the end of each file with sed?
67933
My question is how can I convert all text from uppercase to lowercase and vice versa? That is to change the cases of all the letters. It has to be done with a sed replacement somehow.
62598
How to replace the third occurrence of the string in the file using sed command. Example: Change only the third occurrence of is to us in the file. My input file contains: hai this is linux. hai this is unix. hai this is mac. hai this is unchanged.  The expected output is: hai this is linux. hai thus is unix. hai this us mac. hai this is unchanged.
67607
What's the difference between executing multiple commands with &amp;&amp; and ;?  Examples:  echo "Hi\!" &amp;&amp; echo "How are you?"   and  echo "Hi\!"; echo "How are you?"