Why is Awk messing up my script variables
Clash 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
bash awk variable
add a comment |Â
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
bash awk variable
try replacing in your codea2="$(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
add a comment |Â
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
bash awk variable
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
bash awk variable
edited Aug 31 at 13:49
asked Aug 31 at 12:54
Sepero
52531226
52531226
try replacing in your codea2="$(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
add a comment |Â
try replacing in your codea2="$(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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Aug 31 at 13:43
schweik
1804
1804
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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