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 *****
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 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. :)
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!
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?
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 }'
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.
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 44:07:48 |
Calls: | 10,392 |
Files: | 14,066 |
Messages: | 6,417,248 |