Why is Awk messing up my script variables

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I really don't know why my variables are getting messed up, but I suspect the cause is awk. I've condensed this problem down to a small script. I'm sorry I can't explain the problem better, but I literally don't know what is going on here. So here's the code:



#!/usr/bin/env bash

QUE_FILE="/mnt/drive4-4/private/queue2.txt"
t1="$(cat "$QUE_FILE")"
echo "$t1"
echo "============"
echo "$t1" | while read i; do
a1="$(echo "$i" | awk 'print $1')"
a2="$(echo "$i" | awk 'print $2')"
echo "a1 $a1 - a2 $a2"
combined="$a1 $a2 11111111111"
echo "combined $combined"
done


And the output:



AA BB CC
DD EE
============
a1 AA - a2 BB
combined AA BB 11111111111
a1 DD - a2 EE
11111111111EE


The last line there should be:



combined DD EE 11111111111









share|improve this question























  • try replacing in your code a2="$(echo "$i" | awk 'print $2') | tr -d 'n' "
    – Rui F Ribeiro
    Aug 31 at 13:01






  • 7




    Check for DOS-style (CRLF) line endings in your file
    – steeldriver
    Aug 31 at 13:01














up vote
1
down vote

favorite












I really don't know why my variables are getting messed up, but I suspect the cause is awk. I've condensed this problem down to a small script. I'm sorry I can't explain the problem better, but I literally don't know what is going on here. So here's the code:



#!/usr/bin/env bash

QUE_FILE="/mnt/drive4-4/private/queue2.txt"
t1="$(cat "$QUE_FILE")"
echo "$t1"
echo "============"
echo "$t1" | while read i; do
a1="$(echo "$i" | awk 'print $1')"
a2="$(echo "$i" | awk 'print $2')"
echo "a1 $a1 - a2 $a2"
combined="$a1 $a2 11111111111"
echo "combined $combined"
done


And the output:



AA BB CC
DD EE
============
a1 AA - a2 BB
combined AA BB 11111111111
a1 DD - a2 EE
11111111111EE


The last line there should be:



combined DD EE 11111111111









share|improve this question























  • try replacing in your code a2="$(echo "$i" | awk 'print $2') | tr -d 'n' "
    – Rui F Ribeiro
    Aug 31 at 13:01






  • 7




    Check for DOS-style (CRLF) line endings in your file
    – steeldriver
    Aug 31 at 13:01












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I really don't know why my variables are getting messed up, but I suspect the cause is awk. I've condensed this problem down to a small script. I'm sorry I can't explain the problem better, but I literally don't know what is going on here. So here's the code:



#!/usr/bin/env bash

QUE_FILE="/mnt/drive4-4/private/queue2.txt"
t1="$(cat "$QUE_FILE")"
echo "$t1"
echo "============"
echo "$t1" | while read i; do
a1="$(echo "$i" | awk 'print $1')"
a2="$(echo "$i" | awk 'print $2')"
echo "a1 $a1 - a2 $a2"
combined="$a1 $a2 11111111111"
echo "combined $combined"
done


And the output:



AA BB CC
DD EE
============
a1 AA - a2 BB
combined AA BB 11111111111
a1 DD - a2 EE
11111111111EE


The last line there should be:



combined DD EE 11111111111









share|improve this question















I really don't know why my variables are getting messed up, but I suspect the cause is awk. I've condensed this problem down to a small script. I'm sorry I can't explain the problem better, but I literally don't know what is going on here. So here's the code:



#!/usr/bin/env bash

QUE_FILE="/mnt/drive4-4/private/queue2.txt"
t1="$(cat "$QUE_FILE")"
echo "$t1"
echo "============"
echo "$t1" | while read i; do
a1="$(echo "$i" | awk 'print $1')"
a2="$(echo "$i" | awk 'print $2')"
echo "a1 $a1 - a2 $a2"
combined="$a1 $a2 11111111111"
echo "combined $combined"
done


And the output:



AA BB CC
DD EE
============
a1 AA - a2 BB
combined AA BB 11111111111
a1 DD - a2 EE
11111111111EE


The last line there should be:



combined DD EE 11111111111






bash awk variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 at 13:49

























asked Aug 31 at 12:54









Sepero

52531226




52531226











  • try replacing in your code a2="$(echo "$i" | awk 'print $2') | tr -d 'n' "
    – Rui F Ribeiro
    Aug 31 at 13:01






  • 7




    Check for DOS-style (CRLF) line endings in your file
    – steeldriver
    Aug 31 at 13:01
















  • try replacing in your code a2="$(echo "$i" | awk 'print $2') | tr -d 'n' "
    – Rui F Ribeiro
    Aug 31 at 13:01






  • 7




    Check for DOS-style (CRLF) line endings in your file
    – steeldriver
    Aug 31 at 13:01















try replacing in your code a2="$(echo "$i" | awk 'print $2') | tr -d 'n' "
– Rui F Ribeiro
Aug 31 at 13:01




try replacing in your code a2="$(echo "$i" | awk 'print $2') | tr -d 'n' "
– Rui F Ribeiro
Aug 31 at 13:01




7




7




Check for DOS-style (CRLF) line endings in your file
– steeldriver
Aug 31 at 13:01




Check for DOS-style (CRLF) line endings in your file
– steeldriver
Aug 31 at 13:01










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










while read A1 A2 REST; do echo -e "a1 $A1 - a2 $A2 ncombined $A1 $A2 111111" ; done < /mnt/drive4-4/private/queue2.txt


The diference is that awk prints the last on the row field with 'end of line', while the read AA BB CC saves the particular fields without 'end of line'. The REST saves the third (if any) and all the next fields. In echo -e the n breaks to the new line.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466030%2fwhy-is-awk-messing-up-my-script-variables%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    while read A1 A2 REST; do echo -e "a1 $A1 - a2 $A2 ncombined $A1 $A2 111111" ; done < /mnt/drive4-4/private/queue2.txt


    The diference is that awk prints the last on the row field with 'end of line', while the read AA BB CC saves the particular fields without 'end of line'. The REST saves the third (if any) and all the next fields. In echo -e the n breaks to the new line.






    share|improve this answer
























      up vote
      3
      down vote



      accepted










      while read A1 A2 REST; do echo -e "a1 $A1 - a2 $A2 ncombined $A1 $A2 111111" ; done < /mnt/drive4-4/private/queue2.txt


      The diference is that awk prints the last on the row field with 'end of line', while the read AA BB CC saves the particular fields without 'end of line'. The REST saves the third (if any) and all the next fields. In echo -e the n breaks to the new line.






      share|improve this answer






















        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        while read A1 A2 REST; do echo -e "a1 $A1 - a2 $A2 ncombined $A1 $A2 111111" ; done < /mnt/drive4-4/private/queue2.txt


        The diference is that awk prints the last on the row field with 'end of line', while the read AA BB CC saves the particular fields without 'end of line'. The REST saves the third (if any) and all the next fields. In echo -e the n breaks to the new line.






        share|improve this answer












        while read A1 A2 REST; do echo -e "a1 $A1 - a2 $A2 ncombined $A1 $A2 111111" ; done < /mnt/drive4-4/private/queue2.txt


        The diference is that awk prints the last on the row field with 'end of line', while the read AA BB CC saves the particular fields without 'end of line'. The REST saves the third (if any) and all the next fields. In echo -e the n breaks to the new line.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 31 at 13:43









        schweik

        1804




        1804



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466030%2fwhy-is-awk-messing-up-my-script-variables%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay