Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 202
M__ Avatar asked Dec 31 '22 16:12

M__


2 Answers

You could use printf with the whole file as the formatting string:

printf "$(< file.txt)\n" one two three

Notice that command substitution removes trailing newlines, so a separate \n is added back; see How to avoid bash command substitution to remove the newline character? for details.

like image 198
Benjamin W. Avatar answered Jan 02 '23 06:01

Benjamin W.


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.

like image 24
tshiono Avatar answered Jan 02 '23 05:01

tshiono