7
1
The following inserting file method for sed
used to be working, even before last line, but not any more. Is it a bug in the current sed
?
Demo of inserting file method with sed:
mkdir /tmp/test
printf '%s\n' {1..3} > /tmp/test/f1
printf '%s\n' {one,two,three,four,five,six,seven,eight,nine,ten} > /tmp/test/f2
$ cat /tmp/test/f2 | sed -e "/nine/r /tmp/test/f1" -e //N
one
two
three
four
five
six
seven
eight
1
2
3
nine
ten
$ head -9 /tmp/test/f2 | sed -e "/nine/r /tmp/test/f1" -e //N
one
two
three
four
five
six
seven
eight
nine
1
2
3
$ cat /tmp/test/f2 | sed -e "/ten/r /tmp/test/f1" -e //N
one
two
three
four
five
six
seven
eight
nine
ten
1
2
3
$ sed --version
GNU sed version 4.2.1
...
I.e., the inserting file before the line method works everywhere except the last line. It used to be working before. Is it a bug in the current sed
?
Thanks
I've been trying hard to achieve it without success. I would switch to
awk
orperl
, but just curiosity, which was the previous working version? – Birei – 2014-07-11T08:52:07.277I don't quite understand the mechanics of //N. But if you want to prepend text before the last line, why not just append it to the line before last? So, skip the //N then it will always append. Now you want text from file f1 before the last line
$ echo -e 'one\ntwo\nthree\nfour' | sed -e '/three/r ./f1'
– barlop – 2014-07-11T13:22:07.323"why not just append it to the line before last?" The problem is that you can't predict what the line before last would be, so I HAVE TO match against the last line, then insert before it. @Birei, I didn't keep the version, neither I kept the time I test it. But I dig the above from my note, which had been working before. – xpt – 2014-07-11T14:08:07.800
@xpt I agree it'd be better to be more generic but you are predicting the last line because in your example you are stating it explicitly /ten/ so knowing beforehand that 'ten' was the last line. – barlop – 2014-07-12T13:26:20.737
Exactly @barlop. Just like an html file, you don't know what its contents are, but a well formed html file would always have '
</body>
' as the last time. Just an e.g. – xpt – 2014-07-30T03:27:38.340