I have a problem with my script.
Prelude Firstly, I have a list, 100 lines-file like that:
100;TEST ONE
101;TEST TWO
...
200;TEST HUNDRED
Each line have 2 arguments. For example, first line's arguments are: "645", "TEST ONE". So semicolon is a delimiter.
I need to put both arguments in two variables. Let's say it will be $id and $name. For each line, $id and $name values will be different. For example, for second line $id = "646" and $name = "TEST TWO".
After that I need to take the sample file and change predefined keywords to $id and $name values. Sample file looks like this:
xxx is yyy
And as a result I want to have 100 files with different content. Each file must contain $id and $name data from every line. And It must be named by it's $name value.
There is my script:
#!/bin/bash -x
rm -f output/*
for i in $(cat list)
do
id="$(printf "$i" | awk -F ';' '{print $1}')"
name="$(printf "$i" | awk -F ';' '{print $2}')"
cp sample.xml output/input.tmp
sed -i -e "s/xxx/$id/g" output/input.tmp
sed -i -e "s/yyy/$name/g" output/input.tmp
mv output/input.tmp output/$name.xml
done
So, I just try to read my list file line by line. For every line I'm getting two variables and then use them to replace keywords (xxx and yyy) from sample file and then save result.
But something went wrong
As a result I have only 1 output file. And debug is looking bad.
Here is debug window with only 2 lines in my list file. I got only one output file. File name is just "TEST" and it contain a string: "101 is TEST".
Two files expected: "TEST ONE", "TEST TWO" and it must contain "100 is TEST ONE" and "101 is TEST TWO".
As you can see, second variable have a space in it ("TEST ONE" for example). I think the issue is related to the space special symbol, but I don't know why. I put -F awk parameter to ";", so awk must interpret only semicolon as a separator!
What I did wrong?
for
loop into awhile IFS= read -r line
loop and your input file to a Unix text file. – Kusalananda Apr 08 '20 at 18:22cat
andls
, as these ugly (and error-prone) things pop up nearly every day? – rexkogitans Apr 09 '20 at 06:48cat
but with the way the shell splits the result of a command substitution, and relevant Q/A is https://unix.stackexchange.com/questions/108963 For the case withls
, there is https://unix.stackexchange.com/questions/128985 The Q/A at https://unix.stackexchange.com/questions/131766 is a good overall reference. Many users seems to also refer to Greg Wooledge's Wiki: https://mywiki.wooledge.org/BashPitfalls – Kusalananda Apr 09 '20 at 07:15