Is there a program that functions like printf with an entire file as input?

Viewed 170

My use case is as follows.

file.txt

First arg: %s
Second %s arg,
Third %s

Run prog file.txt "one" "$(ls dir1/dir2)" "three"

Resulting in

First arg: one
Second file1 file2 file3 arg,
Third three

I first thought to use sed but that inevitably runs into problems when bash substitutions get read as format characters like this sed s/%1/$VAR/g -> sed s/%1/dir1/dir2/file1/g

I'm thinking I could iterate by lines and take the text before and after a substitute and just insert like

FIRST=$(sed "s/%s.*//" <<< $LINE)
SECND=$(sed "s/.*%s//" <<< $LINE)

echo "$FIRST $SUB $SECND"

Though that's limited to one sub per line and I would prefer a cleaner solution if it exists.

2 Answers

Would you please try the following as prog:

#!/bin/bash

c=1                                     # counter for argv
nl=$'\n'                                # newline character
while IFS= read -r format; do           # read "file.txt" line by line
    (( c++ ))                           # increment the argv counter
    argv="${!c}"                        # obtain the argv indexed by "c"
    printf "${format}\n" "${argv//$nl/ }"
                                        # print the argv formatted by the line of "file.txt"
done < "$1"                             # "$1" is assigned to "file.txt"

The substitution "${argv//$nl/ }" just converts the newline characters included in the output of ls into whitespace characters.

Related