Awk of string followed by 'n'

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I try to parse a plain text config file A.conf with the following content:
bitrate 500000
use_can 1
I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
Here is the corresponding part of my shell script:
bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
sudo /sbin/ip link set can0 up type can bitrate $bitrate
It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
In that case I get this error output:
" is wrong: invalid "bitrate" value
What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.
text-processing awk grep newlines
add a comment |Â
up vote
0
down vote
favorite
I try to parse a plain text config file A.conf with the following content:
bitrate 500000
use_can 1
I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
Here is the corresponding part of my shell script:
bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
sudo /sbin/ip link set can0 up type can bitrate $bitrate
It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
In that case I get this error output:
" is wrong: invalid "bitrate" value
What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.
text-processing awk grep newlines
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I try to parse a plain text config file A.conf with the following content:
bitrate 500000
use_can 1
I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
Here is the corresponding part of my shell script:
bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
sudo /sbin/ip link set can0 up type can bitrate $bitrate
It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
In that case I get this error output:
" is wrong: invalid "bitrate" value
What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.
text-processing awk grep newlines
I try to parse a plain text config file A.conf with the following content:
bitrate 500000
use_can 1
I want to use the number behind bitrate to setup a CAN-interface with the appropriate bitrate.
Here is the corresponding part of my shell script:
bitrate="$(cat /home/pi/A.conf | grep 'bitrate' | awk 'print $2')"
sudo /sbin/ip link set can0 up type can bitrate $bitrate
It works fine, if there is at least one empty space character after the number 500000, but not if it is immediately followed by a newline.
In that case I get this error output:
" is wrong: invalid "bitrate" value
What is the reason for this behaviour and how can I fix it? I don't want to rely on the fact that each config file needs empty space characters before each new line.
text-processing awk grep newlines
text-processing awk grep newlines
edited Aug 13 at 10:04
Jeff Schaller
32.5k849110
32.5k849110
asked Aug 13 at 9:58
oh.dae.su
171118
171118
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.
bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"
Notice also how this does away with the useless cat and the useless grep.
2
Withouttr:bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
â Kamil Maciorowski
Aug 13 at 10:13
2
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
It would be more natural imho to set the record separator to DOS style (e.g.-vRS='rn') rather than addingras a field separator. If you do changeFS, I'd recommend making it[ tr]+to preserve the default behavior of splitting on one or more whitespace characters.
â steeldriver
Aug 13 at 11:07
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
 |Â
show 1 more 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
Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.
bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"
Notice also how this does away with the useless cat and the useless grep.
2
Withouttr:bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
â Kamil Maciorowski
Aug 13 at 10:13
2
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
It would be more natural imho to set the record separator to DOS style (e.g.-vRS='rn') rather than addingras a field separator. If you do changeFS, I'd recommend making it[ tr]+to preserve the default behavior of splitting on one or more whitespace characters.
â steeldriver
Aug 13 at 11:07
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
 |Â
show 1 more comment
up vote
3
down vote
accepted
Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.
bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"
Notice also how this does away with the useless cat and the useless grep.
2
Withouttr:bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
â Kamil Maciorowski
Aug 13 at 10:13
2
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
It would be more natural imho to set the record separator to DOS style (e.g.-vRS='rn') rather than addingras a field separator. If you do changeFS, I'd recommend making it[ tr]+to preserve the default behavior of splitting on one or more whitespace characters.
â steeldriver
Aug 13 at 11:07
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
 |Â
show 1 more comment
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.
bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"
Notice also how this does away with the useless cat and the useless grep.
Your input file apparently contains a DOS carriage return (ctrl-M, octal 15). The sane solution is not not use a Windows editor in the first place; a reasonable workaround is to explicitly remove this character.
bitrate="$(tr -d '15' < /home/pi/A.conf | awk '/bitrate/ print $2')"
Notice also how this does away with the useless cat and the useless grep.
edited Aug 13 at 10:09
answered Aug 13 at 10:06
tripleee
4,84911626
4,84911626
2
Withouttr:bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
â Kamil Maciorowski
Aug 13 at 10:13
2
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
It would be more natural imho to set the record separator to DOS style (e.g.-vRS='rn') rather than addingras a field separator. If you do changeFS, I'd recommend making it[ tr]+to preserve the default behavior of splitting on one or more whitespace characters.
â steeldriver
Aug 13 at 11:07
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
 |Â
show 1 more comment
2
Withouttr:bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"
â Kamil Maciorowski
Aug 13 at 10:13
2
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
It would be more natural imho to set the record separator to DOS style (e.g.-vRS='rn') rather than addingras a field separator. If you do changeFS, I'd recommend making it[ tr]+to preserve the default behavior of splitting on one or more whitespace characters.
â steeldriver
Aug 13 at 11:07
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
2
2
Without
tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"â Kamil Maciorowski
Aug 13 at 10:13
Without
tr: bitrate="$(</home/pi/A.conf awk -F '[ r]' '/bitrate/ print $2')"â Kamil Maciorowski
Aug 13 at 10:13
2
2
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Yeah, though the really proper solution is to not have this stupid character in the file in the first place.
â tripleee
Aug 13 at 10:19
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
Thanks a lot for your help! Both solutions are working perfectly. Unfortunately, I will have to deal with the DOS carriage return, because the config file is created on a Windows machine and downloaded to the Raspberry Pi from the cloud.
â oh.dae.su
Aug 13 at 10:28
It would be more natural imho to set the record separator to DOS style (e.g.
-vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.â steeldriver
Aug 13 at 11:07
It would be more natural imho to set the record separator to DOS style (e.g.
-vRS='rn') rather than adding r as a field separator. If you do change FS, I'd recommend making it [ tr]+ to preserve the default behavior of splitting on one or more whitespace characters.â steeldriver
Aug 13 at 11:07
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
Many Windows editors are perfectly capable of producing valid Unix text files, and many upload tools are able to perform line-ending translation when you upload a text file.
â tripleee
Aug 13 at 11:20
 |Â
show 1 more 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%2f462263%2fawk-of-string-followed-by-n%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