sed s/baz/baz\\nelephants/
You are missing the dash that the line starts with
sed -i.bak s/baz/"baz\\n - elephants"/ *.txt21–30 of 78 posts
go get github.com/orivej/unix/regrep
regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n'
It only processes standard input, and can not by itself replace the contents of an input file with its output; but another tool, inplace, helps: go get github.com/orivej/unix/inplace
find . -name '*.yaml' -exec inplace {} regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' \;Nice article, good to hear ed is not dead. =) You could also just add the text after the matching line. A little simpler and more straight forward. $ cat > /tmp/ed-script /baz a - elephants . w q $ cat /tmp/2 foo: - bar - baz - bananas $ cat /tmp/ed-script | ed /tmp/2 33 - baz 47 $ cat /tmp/2 foo: - bar - baz - elephants - bananas $·
Nice article, good to hear ed is not dead. =) You could also just add the text after the matching line. A little simpler and more straight forward. $ cat > /tmp/ed-script /baz a - elephants . w q $ cat /tmp/2 foo: - bar - baz - bananas $ cat /tmp/ed-script | ed /tmp/2 33 - baz 47 $ cat /tmp/2 foo: - bar - baz - elephants - bananas $·
I don’t think matches the spec that the new line has the same number of leading spaces as the surrounding lines
Earlier quoted context omitted.
I don’t think matches the spec that the new line has the same number of leading spaces as the surrounding lines
Weird, after rereading the article, it seems like I may have imagined that part.
> I had one extra weird requirement which was that some of the lines were indented with 2 spaces, and some with 4 spaces. The - elephant line needed to have the same indentation as the previous line.
In practice I often use Vim instead. :args file1.txt file2.txt file3.txt :set autowrite :argdo norm /- baz/ yypwCelephants The commands set the argument list to a list of three files (by default, it is set to the filenames you passed to vim on the command-line). Then, autowrite is enabled which automatically saves each buffer after editing it. Finally, argdo runs a command on each argument file.
This example searches each HTML file in a directory for a line with a string and then deletes a number of lines:
$ echo "g/search string/ .,+20 d\nx" >> exscript
$ for f in *.html
do
ex - $f This problem is easily solved with a regexp that processes the input as a whole, rather than working on it line by line. I had this need often enough that I exported Go regexp engine as a command line tool regrep, which can insert "elephant" after "baz" with: go get github.com/orivej/unix/regrep regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' It only processes standard input, and can not by itself replace the contents…