With plain bash:
while IFS= read -r word; do printf "${word}%d\\n" {1..4}; done < words.txt
However, putting a variable in the printf format string makes it vulnerable to unexpected characters. For example:
$ cat words.txt
with \n newline
with %s directive
$ while IFS= read -r word; do printf "${word}%d\n" {1..4}; done < words.txt
with
newline1
with
newline2
with
newline3
with
newline4
with 1 directive2
with 3 directive4
Backslash sequences will be interpreted, and %
directives will be obeyed. To protect this, the simple one-line solution becomes:
while IFS= read -r word; do
tmp1=${word//%/%%}
tmp2=${tmp1//\\/\\\\}
printf "${tmp2}%d\\n" {1..4}
done < words.txt
which outputs
with \n newline1
with \n newline2
with \n newline3
with \n newline4
with %s directive1
with %s directive2
with %s directive3
with %s directive4
..
representuser2
anduser3
? Are there any other strings in the original input file? Do we need to skip those? – terdon Dec 02 '22 at 14:29words.txt
? All you want is to make 4 copies of every line in the file adding a number? – terdon Dec 02 '22 at 14:33awk {'print $0 "user"'}
, we see that once in a while and I'm extremely curious - where did you get the idea to put the'
script delimiters inside the body of the script (i.e. inside the{...}
) rather than outside of itawk '{print $0 "user"}'
? Is there a book or a tutorial somewhere suggesting that's the right syntax? – Ed Morton Dec 03 '22 at 22:13