Using AWK to get second column

Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
I can not seem to get the awk command to get the second column of data.
Bash Code:
filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)
printf "$filter_data n"
The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.
The file contents are:
Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s
I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.
bash grep sed awk
add a comment |Â
up vote
9
down vote
favorite
I can not seem to get the awk command to get the second column of data.
Bash Code:
filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)
printf "$filter_data n"
The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.
The file contents are:
Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s
I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.
bash grep sed awk
2
Why save it to variable if you're printing it right away ? Just useawk'sprintf
â Sergiy Kolodyazhnyy
Sep 12 at 21:31
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I can not seem to get the awk command to get the second column of data.
Bash Code:
filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)
printf "$filter_data n"
The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.
The file contents are:
Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s
I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.
bash grep sed awk
I can not seem to get the awk command to get the second column of data.
Bash Code:
filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)
printf "$filter_data n"
The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.
The file contents are:
Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s
I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.
bash grep sed awk
bash grep sed awk
edited Sep 12 at 20:04
dessert
20k55795
20k55795
asked Sep 12 at 19:57
Vlad
483
483
2
Why save it to variable if you're printing it right away ? Just useawk'sprintf
â Sergiy Kolodyazhnyy
Sep 12 at 21:31
add a comment |Â
2
Why save it to variable if you're printing it right away ? Just useawk'sprintf
â Sergiy Kolodyazhnyy
Sep 12 at 21:31
2
2
Why save it to variable if you're printing it right away ? Just use
awk's printfâ Sergiy Kolodyazhnyy
Sep 12 at 21:31
Why save it to variable if you're printing it right away ? Just use
awk's printfâ Sergiy Kolodyazhnyy
Sep 12 at 21:31
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
13
down vote
accepted
You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
Here's how you should pass a shell variable into awk:
filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )
Perfect, thank you.
â Vlad
Sep 12 at 20:13
add a comment |Â
up vote
7
down vote
Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):
re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt
Note that variable quoting isn't necessary when performing assignments in shell
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
Here's how you should pass a shell variable into awk:
filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )
Perfect, thank you.
â Vlad
Sep 12 at 20:13
add a comment |Â
up vote
13
down vote
accepted
You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
Here's how you should pass a shell variable into awk:
filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )
Perfect, thank you.
â Vlad
Sep 12 at 20:13
add a comment |Â
up vote
13
down vote
accepted
up vote
13
down vote
accepted
You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
Here's how you should pass a shell variable into awk:
filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )
You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
Here's how you should pass a shell variable into awk:
filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )
edited Sep 12 at 21:35
Sergiy Kolodyazhnyy
65.8k9134287
65.8k9134287
answered Sep 12 at 20:05
glenn jackman
12.1k2442
12.1k2442
Perfect, thank you.
â Vlad
Sep 12 at 20:13
add a comment |Â
Perfect, thank you.
â Vlad
Sep 12 at 20:13
Perfect, thank you.
â Vlad
Sep 12 at 20:13
Perfect, thank you.
â Vlad
Sep 12 at 20:13
add a comment |Â
up vote
7
down vote
Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):
re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt
Note that variable quoting isn't necessary when performing assignments in shell
add a comment |Â
up vote
7
down vote
Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):
re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt
Note that variable quoting isn't necessary when performing assignments in shell
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):
re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt
Note that variable quoting isn't necessary when performing assignments in shell
Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):
re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt
Note that variable quoting isn't necessary when performing assignments in shell
answered Sep 12 at 21:35
Sergiy Kolodyazhnyy
65.8k9134287
65.8k9134287
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%2faskubuntu.com%2fquestions%2f1074751%2fusing-awk-to-get-second-column%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
2
Why save it to variable if you're printing it right away ? Just use
awk'sprintfâ Sergiy Kolodyazhnyy
Sep 12 at 21:31