How could I store the following BASH command into an Awk variable?
**** begin *****
site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\] SSID\[/\} SSID\{/' \
-e 's/\] channel\[/} channel{/' \
-e 's/\] enc\[/} enc{/' \
-e 's/.$/\}/'
**** end *****
On Thu, 29 Feb 2024 20:18:56 +0800, "Mr. Man-wai Chang" <toylet.toylet@gmail.com> wrote:
How could I store the following BASH command into an Awk variable?
**** begin *****
site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\] SSID\[/\} SSID\{/' \
-e 's/\] channel\[/} channel{/' \
-e 's/\] enc\[/} enc{/' \
-e 's/.$/\}/'
**** end *****
Try:
myvar = "site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\\] SSID\\[/\\} SSID\\{/' \
-e 's/\\] channel\\[/} channel{/' \
-e 's/\\] enc\\[/} enc{/' \
-e 's/.$/\\}/'"
or, if you insist on preserving linebreaks:
myvar2 = "site_survey 2>&1 | \\\n" \
"sed -e 's/^./{/' \\\n" \
"-e 's/\\] SSID\\[/\\} SSID\\{/' \\\n" \
"-e 's/\\] channel\\[/} channel{/' \\\n" \
"-e 's/\\] enc\\[/} enc{/' \\\n" \
"-e 's/.$/\\}/'"
[...]On Thu, 29 Feb 2024 20:18:56 +0800, "Mr. Man-wai Chang"
<toylet.toylet@gmail.com> wrote:
How could I store the following BASH command into an Awk variable?
**** begin *****
site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\] SSID\[/\} SSID\{/' \
-e 's/\] channel\[/} channel{/' \
-e 's/\] enc\[/} enc{/' \
-e 's/.$/\}/'
**** end *****
Thanks!
Because of the call to sed within the Awk vaariable, need to escape:
1. "]" to "\\]"
2. "[" to "\\["
3. "'" to "'\''"
Absolutely horrifying. :)
This is what I end up with:
cmd="site_survey 2>&1 | \
sed -e '\''s/^./{/'\'' \
-e '\''s/\\] SSID\\[/\} SSID\{/'\'' \
-e '\''s/\\] channel\\[/} channel{/'\'' \
-e '\''s/\\] enc\\[/} enc{/'\'' \
-e '\''s/.$/\}/'\'' ";
On 29.02.2024 17:09, Mr. Man-wai Chang wrote:
[...]On Thu, 29 Feb 2024 20:18:56 +0800, "Mr. Man-wai Chang"
<toylet.toylet@gmail.com> wrote:
How could I store the following BASH command into an Awk variable?
**** begin *****
site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\] SSID\[/\} SSID\{/' \
-e 's/\] channel\[/} channel{/' \
-e 's/\] enc\[/} enc{/' \
-e 's/.$/\}/'
**** end *****
Thanks!
Because of the call to sed within the Awk vaariable, need to escape:
1. "]" to "\\]"
2. "[" to "\\["
3. "'" to "'\''"
Absolutely horrifying. :)
This is what I end up with:
cmd="site_survey 2>&1 | \
sed -e '\''s/^./{/'\'' \
-e '\''s/\\] SSID\\[/\} SSID\{/'\'' \
-e '\''s/\\] channel\\[/} channel{/'\'' \
-e '\''s/\\] enc\\[/} enc{/'\'' \
-e '\''s/.$/\}/'\'' ";
Whenever I see this sort of question and the attempted "solution"
I'm tempted to ask whether your approach might be not be the best;
there's quite some indications. - Mind to elaborate on the task
(not technically, but as seen from a higher perspective)?
On 1/3/2024 1:22 am, Janis Papanagnou wrote:
On 29.02.2024 17:09, Mr. Man-wai Chang wrote:
[...]On Thu, 29 Feb 2024 20:18:56 +0800, "Mr. Man-wai Chang"
<toylet.toylet@gmail.com> wrote:
How could I store the following BASH command into an Awk variable?
**** begin *****
site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\] SSID\[/\} SSID\{/' \
-e 's/\] channel\[/} channel{/' \
-e 's/\] enc\[/} enc{/' \
-e 's/.$/\}/'
**** end *****
Thanks!
Because of the call to sed within the Awk vaariable, need to escape:
1. "]" to "\\]"
2. "[" to "\\["
3. "'" to "'\''"
Absolutely horrifying. :)
This is what I end up with:
cmd="site_survey 2>&1 | \
sed -e '\''s/^./{/'\'' \
-e '\''s/\\] SSID\\[/\} SSID\{/'\'' \
-e '\''s/\\] channel\\[/} channel{/'\'' \
-e '\''s/\\] enc\\[/} enc{/'\'' \
-e '\''s/.$/\}/'\'' ";
Whenever I see this sort of question and the attempted "solution"
I'm tempted to ask whether your approach might be not be the best;
there's quite some indications. - Mind to elaborate on the task
(not technically, but as seen from a higher perspective)?
The site_survey command vomits lines:
[ 8] SSID[ [HOME]] BSSID[04:9F:xx:xx:xx:xx] channel[ 6] frequency[2437] numsta[1] rssi[-63] noise[-75] beacon[98] cap[1411]
dtim[0] rate[450] enc[Group-AES-CCMP CCMP PSK2 ]
Just wanna change all field delimiters, which are square brackets, to
curly braces within an Awk program loop.
I agree it might be a silly idea. But this is my first Awk programming
task. :)
Composing a shell command in Awk typically has two (but maybe more) application cases; one is to construct the shell commands to invoke
them from within Awk with the system() function, or to print them
so that you can store them in a file of pipe them into a shell for
direct execution.
It _might_ be better to do these commands in shell instead. (But I
still have not enough information to suggest the best approach.)
You noticed that substitutions with Awk might get nasty if escapes
are necessary for many characters. You may want to reduce the
number of characters to escape by changing the regular expression
that your Sed is using; it can be simplified (by 'or'ing the three
keywords, and likely by also incorporating the last sed expression.
An option might also be to not use Sed here but have an Awk do the substitutions. The escaping complexity can also be reduced if you
put the Sed (or Awk) substitution commands into a file so that you
don't need to do all that ' quoting and escaping.
An option might also be to not use Sed here but have an Awk do the substitutions. The escaping complexity can also be reduced if you
put the Sed (or Awk) substitution commands into a file so that you
don't need to do all that ' quoting and escaping.
On 1/3/2024 10:37 pm, Janis Papanagnou wrote:
Composing a shell command in Awk typically has two (but maybe more)
application cases; one is to construct the shell commands to invoke
them from within Awk with the system() function, or to print them
so that you can store them in a file of pipe them into a shell for
direct execution.
It _might_ be better to do these commands in shell instead. (But I
still have not enough information to suggest the best approach.)
[...]
Because the shell command is to be looped inside the Awk script with
data accumulation and calculation!
Sorry, that description is unclear to me.
(Yet it still sounds to me as you've not chosen
the appropriate separation between Shell and Awk.)
On 2/3/2024 1:29 am, Janis Papanagnou wrote:
Sorry, that description is unclear to me.
(Yet it still sounds to me as you've not chosen
the appropriate separation between Shell and Awk.)
Back to the site_survey example.
You want to count bssid after each call to site_survey, how could you
not loop the output of site_survey inside Awk? Unless you have a way to
save the counts outside Awk?
In article <urut1c$1qhfm$1@toylet.eternal-september.org>,
Mr. Man-wai Chang <toylet.toylet@gmail.com> wrote:
Back to the site_survey example.
You want to count bssid after each call to site_survey, how could you
not loop the output of site_survey inside Awk? Unless you have a way to
save the counts outside Awk?
Just to get some sort of baseline example, is there any reason you can't do it like this:
$ site_survey | awk '...'
where the awk program (the "..." above) does whatever manipulations you
need; i.e., all the substitutions you had previously been doing in "sed".
Note: The real problem here is that none of the people who are trying to
help you (*) have any real idea what your actual program looks like.
(*) A group I now seem to have added myself to.
On 2/3/2024 7:43 pm, Kenny McCormack wrote:
In article <urut1c$1qhfm$1@toylet.eternal-september.org>,
Mr. Man-wai Chang <toylet.toylet@gmail.com> wrote:
Back to the site_survey example.
You want to count bssid after each call to site_survey, how could you
not loop the output of site_survey inside Awk? Unless you have a way to
save the counts outside Awk?
Just to get some sort of baseline example, is there any reason you
can't do
it like this:
$ site_survey | awk '...'
where the awk program (the "..." above) does whatever manipulations you
need; i.e., all the substitutions you had previously been doing in "sed".
I want to count the bssid after each call to site_survey, accumulative!
To do that outside of Awk '...', you need a place to store the counts
from last call to site_survey.
#
# something like this:
#
n[bssid]=0
for (;;) {
count_bssid(site_survey, n)
endfor
print n[bssid]
On 03.03.2024 11:14, Mr. Man-wai Chang wrote:
On 2/3/2024 7:43 pm, Kenny McCormack wrote:
In article <urut1c$1qhfm$1@toylet.eternal-september.org>,I want to count the bssid after each call to site_survey, accumulative!
Mr. Man-wai Chang <toylet.toylet@gmail.com> wrote:
Back to the site_survey example.
You want to count bssid after each call to site_survey, how could you
not loop the output of site_survey inside Awk? Unless you have a way to >>>> save the counts outside Awk?
Just to get some sort of baseline example, is there any reason you
can't do
it like this:
$ site_survey | awk '...'
where the awk program (the "..." above) does whatever manipulations you
need; i.e., all the substitutions you had previously been doing in "sed". >>
I don't see how the previous construction of a sed command helps here.
It's still fuzzy to me what you want, so below I can give you some
informal hints and once you've chosen which way you want to go you
can come back with more specific questions.
One general way of doing such things is (informally written)
data_source | awk '{ x=extract-data() ; c+=x } END { print c }'
As you say it's not very clear what is needed, but I think the key pointd[something] += x }
is that the data must be accumulated. The basic pattern is probably
then more like
data_source | awk \
'BEGIN { d = read_saved_data(); } { x=extract-data() ; d[something] += x }
END { write_data(d); }'
though there may be data output as well. In fact it's possible that the simplest way to store the accumulating data is to output it and use tee:
data_source | awk \
'BEGIN { d = read_saved_data(); } { x=extract-data() ;
END { print lots of stats...; }' | tee saved_data
On 4/3/2024 1:17 am, Ben Bacarisse wrote:
As you say it's not very clear what is needed, but I think the key pointx }
is that the data must be accumulated. The basic pattern is probably
then more like
data_source | awk \
'BEGIN { d = read_saved_data(); } { x=extract-data() ; d[something] += x }
END { write_data(d); }'
though there may be data output as well. In fact it's possible that the
simplest way to store the accumulating data is to output it and use tee:
data_source | awk \
'BEGIN { d = read_saved_data(); } { x=extract-data() ; d[something] +=
END { print lots of stats...; }' | tee saved_data
That's exactly what I was trying to do. How do you store and restore the array of counts[bssid]? Flatten the array into Bash variables?
I think it's not too bad to do everything within the Awk script, given
Awk's capabilties.
How could I store the following BASH command into an Awk variable?
**** begin *****
site_survey 2>&1 | \
sed -e 's/^./{/' \
-e 's/\] SSID\[/\} SSID\{/' \
-e 's/\] channel\[/} channel{/' \
-e 's/\] enc\[/} enc{/' \
-e 's/.$/\}/'
**** end *****
Do not do this. You wouldn't do it in shell (see https://mywiki.wooledge.org/BashFAQ/050) and you definitely must not do
this in awk as that'll have all of the issues associated with storing
code in a shell variable PLUS additional issues of robustness and
efficiency related to spawning a subshell to call an external tool from awk.
Whatever it is you're trying to do, post a new question with a minimal complete, verifiable example (see https://stackoverflow.com/help/minimal-reproducible-example for what
that means) so we can help you do it the right way.
Already done so. My question in the end was more about escaping >special/reserved symbols.
In article <usc4tk$10akp$1@toylet.eternal-september.org>,
Mr. Man-wai Chang <toylet.toylet@gmail.com> wrote:
...
Already done so. My question in the end was more about escaping
special/reserved symbols.
Well, not really.
At this point, I still don't think anyone else reading/posting to this
thread has any real idea what you are actually trying to do.
Now, it is certainly your right not to tell us, and we don't really have
any right to insist that you do, but it does limit the amount of help we
can provide. FWIW (and that might not be much), I think most of us are having a reaction of "Whatever it is you're trying to do, this is not the right way to do it".
Now, to be fair, you did give a slight hint as to what this was really
about, somewhere upthread, where you indicated that this was a program that you got from someone/somewhere on the net and you just needed to modify it slightly to make it do what you wanted. In that case, an "I just need help with one simple thing" type answer might actually be appropriate. Again FWIW, I think most of the help-givers on this thread have assumed that this was your own program and that you are developing it from scratch.
Now, it is certainly your right not to tell us, and we don't really have
any right to insist that you do, but it does limit the amount of help we
can provide. FWIW (and that might not be much), I think most of us are having a reaction of "Whatever it is you're trying to do, this is not the right way to do it".
Now, to be fair, you did give a slight hint as to what this was really
about, somewhere upthread, where you indicated that this was a program that you got from someone/somewhere on the net and you just needed to modify it slightly to make it do what you wanted. In that case, an "I just need help with one simple thing" type answer might actually be appropriate. Again FWIW, I think most of the help-givers on this thread have assumed that this was your own program and that you are developing it from scratch.
On 6/3/2024 9:03 pm, Ed Morton wrote:
Do not do this. You wouldn't do it in shell (see
https://mywiki.wooledge.org/BashFAQ/050) and you definitely must not do
this in awk as that'll have all of the issues associated with storing
code in a shell variable PLUS additional issues of robustness and
efficiency related to spawning a subshell to call an external tool
from awk.
I undertsand the limitations of Awk. But I was wondering about how far I could go. :)
Whatever it is you're trying to do, post a new question with a minimal
complete, verifiable example (see
https://stackoverflow.com/help/minimal-reproducible-example for what
that means) so we can help you do it the right way.
Already done so. My question in the end was more about escaping special/reserved symbols.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,030 |
Nodes: | 10 (1 / 9) |
Uptime: | 203:46:16 |
Calls: | 13,341 |
Calls today: | 4 |
Files: | 186,574 |
D/L today: |
4,133 files (1,293M bytes) |
Messages: | 3,357,167 |