Unix & Linux
sed filenames parameter
Updated Wed, 15 Jun 2022 04:56:54 GMT

print empty line in sed output when passing multiple filenames


I'm doing some placeholder replacements in various files that I'm feeding to ldapadd, to add new entries to an LDAP directory:

sed \
    -e 's/%%FOO%%/whatever/g' \
    -e 's/%%BAR%%/other thing/g \
    file1.ldif.template \
    file2.ldif.template \
    | ldapadd -x -D 'cn=admin,dc=example,dc=com' -W

The problem I'm having with this is that if there is not an empty line at the end of file1.ldif.template, the first record in file2 is concatenated to the last record in file1, and in ldif files different records should be separated by at least 1 newline.

Of course I could add an empty line at the end of file1, but that is very easy to fail in the future if some other developer (or their editors) remove the trailing newlines.

So to summarize. Current (simplified) sed output:

dn: cn=record1_file1,dc=example,dc=com
cn: record1_file1
dn: cn=record2_file1,dc=example,dc=com
cn: record2_file1
dn: cn=record1_file2,dc=example,dc=com
cn: record1_file2
dn: cn=record2_file2,dc=example,dc=com
cn: record2_file2

Desired (simplified) output:

dn: cn=record1_file1,dc=example,dc=com
cn: record1_file1
dn: cn=record2_file1,dc=example,dc=com
cn: record2_file1
dn: cn=record1_file2,dc=example,dc=com
cn: record1_file2
dn: cn=record2_file2,dc=example,dc=com
cn: record2_file2

I'm working on linux (fedora 21) using GNU sed. Portability is not a concern (but i'll prefer a portable solution over a GNU solution).




Solution

Using GNU sed -s (--separate) extension, you can append an empty newline after each filename (line addresses refer to each filename, instead of treating all the input as one longer stream, similar to awk's FNR and NR variables)

sed \
    -s \
    -e '$a\\' \
    -e 's/%%FOO%%/whatever/g' \
    -e 's/%%BAR%%/other thing/g \
    file1.ldif.template \
    file2.ldif.template \
    | ldapadd -x -D 'cn=admin,dc=example,dc=com' -W

So using -s with -e '$a\\' is what makes sed insert a newline at the end of all input files.