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?
1394
Replacing strings in files based on certain search criteria is a very common task. How can I    replace string foo with bar in all files in the current directory? do the same recursively for sub directories? replace only if the file name matches another string? replace only if the string is found in a certain context? replace if the string is on a certain line number? replace multiple strings with the same replacement replace multiple strings with different replacements
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.
21864
I've been using sed for quite some time but here is a quirk I came around with, which I am not able to resolve.   Let me explain my problem with the actual case.    Scene#1  printf "ls" | xclip -selection clipboard echo "ls" | xclip -selection clipboard   In the first command, I pipe printf output to xclip so that it gets copied to the clipboard. Now, printf, unlike echo does not insert a new line at the end by default. So, if I paste this content into terminal, the ls command that is copied does not automatically run.   In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.   This is undesirable for me. So, I wanted to remove the newline using sed, but it failed, as explained in the scene below.  Scene#2  echo "ls" | sed -r 's/\n//g' | xclip -selection clipboard   The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.   I also tried removing carriage return character \r. But nada. It seems I am missing something very crucial/basic here.
97125
I have the following string: /tmp/test/folder1/test.txt  I wish to use sed to substitute / for \/ - for example:  \/tmp\/test\/folder1\/test.txt   So I issue:  echo "/tmp/test/folder1/test.txt" | sed "s/\//\\\\//g"   Although it returns:     sed: -e expression #1, char 9: unknown option to `s'   I am escaping the forward slash and backslash - so not sure where I have gone wrong here
50199
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 sed command:  sed -i '2s/^.*$/X/' a.txt   to change the B to X, for example.  But now suppose that I want to write an sed command that changes the text on line 2 to something that includes a bash string variable.  I want to change the text on line 2 to fname="myaddress", where the quotation marks are included explicitly, and where myaddress is a bash string variable (defined somewhere else).  To try to do this, I have created a bash shell script called mytest.sh and containing the following:  #!/bin/bash  myaddress="/home/" echo fname=\"$myaddress\"  head a.txt sed -i '2s/^.*$/fname=\"$myaddress\"/' a.txt head a.txt   Now, echo fname=\"$myaddress\" prints fname="/home/", which is the text that I want to be on line 2 of a.txt.  However, sed instead prints fname="$myaddress" to line 2 of a.txt, probably because it is inside the surrounding apostrophes.  In other words, the output of the above bash shell script is:   fname="/home/" A B C  A fname="$myaddress" C   My question is, how can I get sed to actually print the value stored in myaddress and referenced by $myaddress?
120262
I've got text with whitespace (including a newline) that I need to replace. I'd like to use sed, but AIX doesn't seem to support the  to sed newline questions I found. The version of bash I'm using is "GNU bash, version 3.00.16(1)-release (powerpc-ibm-aix5.1)"  If I run a command that outputs a bunch of pairs of lines of text like:  alias:  aliasname                 10:00:00:00:00:00:00:00   What is the best way to make it one line for each? If I use ssh user@system command | tr '\n' ' ', it replaces both newlines, and I need to keep the last one or else the output goes into a single line.  Edit: what I've tried so far is:   | sed -e ':1' -e 'N' -e '$!b1' -e 's/\n/ /g' which replaces all the newlines, putting the entire output stream into a single line. | while read i; do printf "%s " $i; done; echo which also replaces all the newlines | tr -d '\n' and | tr -d '\n'; echo, which still replace all newlines, however the echo adds one back at the last command.
72768
I'm trying to execute this command  sed -i -e "s/BASE_64/$BASE_64/" FILE_NAME   where $BASE_64 is a base 64 representation of a file content.  sed gives me an error since the string is too long.     Argument list too long   How is it possible to avoid this error?
17675
I used   history | less   to get the lines of previous commands and from the numbers on the left hand side I found the line I wanted repeated (eg. 22) and did  !22   at the command prompt and it worked -- executing the set of commands on the line I did at that time. I cannot figure out where the exclamation mark is used, what does it represent in terms of actions taken by bash, and where to use it. From the documentation I do not see an explanation that is 'tangible'.
25907
Original file claudio antonio claudio michele  I  want to change only the first occurrence of &quot;claudio&quot; with &quot;claudia&quot; so that I would get the following: claudia antonio claudio michele  I have tried the following: sed -e '1,/claudio/s/claudio/claudia/' nomi  The above command performs global substitution (it replaces all occurrences of 'claudio') . Why?
55053
I would like to know what it really does with following scripts used with sed command.  sed -e 's:&lt;F0_M&gt;:&lt;o,f0,male&gt;:' \           -e 's:&lt;F0_F&gt;:&lt;o,f0,female&gt;:' \           -e 's:([0-9])::g' \           -e 's:&lt;sil&gt;::g' \           -e 's:([^ ]*)$::' | \   First and second scripts looks like we are transforming text of type &lt;F0_F&gt; to &lt;o,f0,female&gt;. But what about the last three where it involves '::','g' and '$' sign. In most of the documentations they have used '\' and '/' in most of scripts. But here they have used ':' instead of slashes. Can some one explain above three scripts?
55291
Q: What command would you give to print (display on the screen) the corrected text of userNotes.txt with all sentence endings having required two blanks before the start of the next sentence?  here is my code  sed 's/.\s/.\s\s/'    but this also changed the line where it ended with dot and 2 spaces to dot and 3 spaces.
51340
I want to watch a file until a bit of text appears  I found this answer:   but when I tried it on ubuntu, it doesn't exit:  $ echo one &gt; test.txt $ echo two &gt;&gt; test.txt $ echo three &gt;&gt; test.txt $ echo four &gt;&gt; test.txt $ cat test.txt | sed '/w/ q' one two $   works as expected.  however when I try to tail the file  $ tail -f test.txt | sed '/w/ q' one two   it never exits.  tail does not stop even though the pipe is broken.  Does anybody know how to make tail exit when sed exits?
123973
Typing /usr/bin/env sed -f in terminal works.  But if use it as a shebang,   #!/usr/bin/env sed -f  s/a/b/   The script will be fail to execute:  /usr/bin/env: sed -f: No such file or directory   I kind of believe that it's related with the -f. But how to resolve this?
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?
42171
A lot of command-line utilities can take their input either from a pipe or as a filename argument. For long shell scripts, I find starting the chain off with a cat makes it more readable, especially if the first command would need multi-line arguments.  Compare  sed s/bla/blaha/ data \ | grep blah \ | grep -n babla   and  cat data \ | sed s/bla/blaha/ \ | grep blah \ | grep -n babla   Is the latter method less efficient? If so, is the difference enough to care about if the script is run, say, once a second? The difference in readability is not huge.
48554
I've noticed that, if I add \n to a pattern for substituting using sed, it does not match. Example:  $ cat &gt; alpha.txt This is a test Please do not be alarmed  $ sed -i'.original' 's/a test\nPlease do not/not a test\nBe/' alpha.txt  $ diff alpha.txt{,.original}  $ # No differences printed out   How can I get this to work?
96362
Recently, I asked a  how to sort pairs of lines, and one of the answers suggested concatenating lines with sed, like this:  cat file.txt | sed -n 'N;s/\n//;p' | sort -t";" -k43,43n | perl -F';' -ane '$,=";";print @F[0..13],"\n";print @F[14..$#F]'   which worked brilliantly, but now my problem generalized to sorting n-tupels of lines, which I can't figure out how to do with sed.   Everything I found was either for 2 lines or ALL lines, but I need n lines (where n is 5 at the moment, but a general way would be great).  Bonus points for a nice way of rewriting the perl part to accomodate n lines, but the problem is really about the sed part.  I am also not hung on sed specifically, so if you have a nice solution using a different command line tool, please post it.  Update: Example input (n == 3)  a1;b1;c1;  n1;m1;l1;  d1;e1;f1;g1 n2;m2;l2; a2;b2;c2; d2;e2;f2;g2