Discussion:
[BUG] sed: command "r" wrong behaviour
(too old to reply)
Xose Vazquez Perez
2014-10-02 10:27:51 UTC
Permalink
hi,

- First case with command "i", it works OK.
$ cat f.txt
1
2
3
$ sed -i '1 i\Hello' f.txt
$ cat f.txt
Hello
1
2
3

- Second case with command "r", It doesn't work.
$ cat f2.txt
1
2
3
$ cat h.txt
Hello
$ sed -i '1 r h.txt' f2.txt
$ cat f2.txt
1
Hello
2
3

In second case the line with "Hello" must be the first one,
like the first case with the command "i"

-thanks-
Xose Vazquez Perez
2014-10-02 10:30:01 UTC
Permalink
Post by Xose Vazquez Perez
hi,
- First case with command "i", it works OK.
$ cat f.txt
1
2
3
$ sed -i '1 i\Hello' f.txt
[...]
It happens with 4.2.2 and git versions.
Davide Brini
2014-10-02 13:15:48 UTC
Permalink
On Thu, 02 Oct 2014 12:27:51 +0200, Xose Vazquez Perez
Post by Xose Vazquez Perez
hi,
- First case with command "i", it works OK.
$ cat f.txt
1
2
3
$ sed -i '1 i\Hello' f.txt
$ cat f.txt
Hello
1
2
3
- Second case with command "r", It doesn't work.
$ cat f2.txt
1
2
3
$ cat h.txt
Hello
$ sed -i '1 r h.txt' f2.txt
$ cat f2.txt
1
Hello
2
3
In second case the line with "Hello" must be the first one,
like the first case with the command "i"
No, read the definition of "r" (emphasis mine):

"Queue the contents of FILENAME to be read and inserted into the
output stream ****at the end of the current cycle****, or when the next
input line is read."
--
D.
Andreas Schwab
2014-10-02 13:26:40 UTC
Permalink
Post by Xose Vazquez Perez
- Second case with command "r", It doesn't work.
$ cat f2.txt
1
2
3
$ cat h.txt
Hello
$ sed -i '1 r h.txt' f2.txt
$ cat f2.txt
1
Hello
2
3
In second case the line with "Hello" must be the first one,
like the first case with the command "i"
The r command works like the a command, by *appending* the contents to
the current line, before the *next* line is read.

Andreas.
--
Andreas Schwab, ***@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Loading...