sed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

New feature suggestion: "r" command to accept address zero


From: Assaf Gordon
Subject: New feature suggestion: "r" command to accept address zero
Date: Wed, 11 Aug 2021 13:27:21 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0

Hello,

I'd like to suggest a small feature:

====
The 'r' command can be used with address zero, effectively prepending
a file to the beginning of the input file, e.g.:
    sed '0rA.TXT' B.TXT > C.TXT
is equivalent to:
    cat A.TXT B.TXT > C.TXT

With "sed -i", this allows safe in-place prepending of files.
A typical example would be adding a license header to multiple source
files:
    sed -i '0rLICENSE' *.c *.h
    find -iname '*.cpp' | xargs sed -i '0rLICENSE'

Current cumbersome alternatives are:

    sed -i -e 'x;${p;x};1rA.TXT' -e '1d'  B.TXT

    sed -i '1ihello\nworld' B.TXT

    # Or converting a file to an embedded string
    # with "\n" then using "1i" :
    sed -i -e '1i\' -e "$(sed -z 's/\n/\\n/g' A.TXT)" B.txt

=====

With "-i" this addresses several of the difficulties in Pádraig's document: https://www.pixelbeat.org/docs/unix_file_replacement.html

And also does not require shell scripting, something like:
    cat A.TXT B.TXT > C.TXT && MV C.TXT B.TXT

I seem to remember talks about adding a generic coreutils program
'inplace', but until then, this could be a helpful alternative.


Comments very welcomed,
 - assaf




Attachment: 0001-sed-allow-0rFILE-insert-FILE-before-the-first-line.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]