2

I am trying to parse multiple awk variables into a for loop. I have a document that consists of multiple fields separated by a comma. Each field is capture by awk and I want to use these variables to create multiple instances of text in an additional file. I have used "while" to capture the variables, but this only runs once.

awk -F ',' '{print $1,$2,$3}' extensions.txt | while read var1 var2 var3; do
            echo " <item context=\"active\" type=\"\" fav=\"false\" mod=\"true\" index=\"0\">
            <number>$var3</number>
            <number_type>sip</number_type>
            <first_name>$var1</first_name>
            <last_name>$var2</last_name>
            <organization>Stackoverflow</organization>
            </item>" > test.txt
done
exit

Output for test.txt is:

        <item context=\"active\" type=\"\" fav=\"false\" mod=\"true\" index=\"0\">
        <number>123456789</number>
        <number_type>sip</number_type>
        <first_name>Jon</first_name>
        <last_name>Doe</last_name>
        <organization>Stackoverflow</organization>
        </item>

If I use a for loop, it won't retain the 3 variables seperately, but rather take the combined output and place it into a single variable.

4
  • The trailing exit is pretty pointless; the shell will exit anyway if you delete the line and it is the last (non-blank, non-comment) line in the script. Commented Jun 28, 2016 at 6:37
  • Note that each iteration of the loop clobbers the file test.txt. Either place the output redirection after, but on the same line as, the done, or use append mode >>. Commented Jun 28, 2016 at 6:38
  • You should really show a few (say 3) lines of input, plus the desired output, plus the actual output. Commented Jun 28, 2016 at 6:39
  • Hard to format the comment, but multiple line echo with embedded quotes is much cleaner written with a heredoc. egcat << EOF\n <item context="active...\nEOF Commented Jun 28, 2016 at 9:22

2 Answers 2

4

You don't need to use awk. You can do it with bash itself by using IFS (Internal Field Separator).

Use this:

while IFS="," read v1 v2 v3 _
do
    echo "<item context=\"active\" type=\"\" fav=\"false\" mod=\"true\" index=\"0\">
    <number>$v1</number>
    <number_type>sip</number_type>
    <first_name>$v2</first_name>
    <last_name>$v3</last_name>
    <organization>Stackoverflow</organization>
    </item>";
done < extensions.txt > output.txt
Sign up to request clarification or add additional context in comments.

2 Comments

I just tested it and it does work indeed. Thanks man appreciate it!!! :)
You might want to add a dummy variable at the end to catch any potential four words lines: read -r v1 v2 v3 _ <<< "a,b,c,d,e,f" Will assign a, b and c to v1, v2 and v3, while d,e,f will be assigned to _.
3

You could let Awk do all the work. Awk does process line by line, so put the awk in a file and use awk -f your_program.awk.

Something like this should work:

{
    print "<item content=\"active\" type=\"\" fav=\"false\" mod=\"true\" index=\"0\">"
    print "<number>"$1"</number>"
    print "<number_type>sip</number_type>"
    print "<first_name>"$2"</first_name>"
    print "<last_name>"$3"</last_name>"
    print "<organization>Stackoverflow</organization>"
    print "</item>"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.