Read from file and replace in format

Hi Guys,

I am having below content in a file which is tab dimited

file.txt

AUS AUS_UP
NZ NZ_UP
ENG ENG_AP

I need to read the file content and replace it with below output format one to one replace

try("AUS ").ss("AUS_UP")
try("NZ ").ss("NZ_UP")
try("ENG ").ss("ENG_AP")

With 41 other requests for help over the last 3 and a half years, we would hope that you have some idea of how to do things like this on your own.

What have you tried to solve this problem?

What operating system are you using?

What shell are you using?

Hi,

I am using sun solaris OS and bash

code i have tried

while read -r i; do
   printf "try("$i ").ss("$i")<%s>\n" 
done < test.txt

try(AUStry(NZtry(ENG1

But not able to break the space dilimted

Note that there are two fields in your input file; not just one.

Note that the format string presented to printf needs to be a single operand and (since double-quotes do not nest), the format string you used is three operands (and the last two were ignored since there is no format specifier in the 1st operand that refers to them).

Try this instead:

while read -r f1 f2
do	printf 'try("%s ").ss("%s")\n' "$f1" "$f2"
done < file.txt

If you want to include double-quotes in the output and you're using double-quotes to delimit the format string you need to escape the inner quotes:

while read -r f1 f2
do	printf "try(\"%s \").ss(\"%s\")\n" "$f1" "$f2"
done < file.txt

but I find using single-quotes to delimit the format string easier to write and easier to read.

1 Like