ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to pass the last command result to an awk variable, so i can use it.
I already do a similar thing with the shell loop.
Quote:
for i in {1..9}; do zcat session.txt.gz | awk -v i=$i ...
Before this i do another command, and i want to pass the result, so that i can use it in awk. How can i pass the 1st command result to awk, and use it as a var(RESULT).
Quote:
zcat session.txt.gz | wc -l
Quote:
for i in {1..9}; do zcat session.txt.gz | awk -v i=$i 'BEGIN{session="1";count=0;sum=0} {if ($1==session) count++; else {if(count == i) sum+=1;session=$1;count=1}} END{if (count == i) sum+=1; print i,"Pesquisa(s): ",sum/RESULT}'; done
On the other hand, if you're using gawk rather than awk, you don't need the shell script at all.
Code:
# Run a command, printing any TTY output.
# Print a message to stderr and exit on failure
# Note: Only the succeed/fail code is returned. See cmd_result() for string/array returns
function run_cmd(\
cmd, # Command to be run\
error_message, # Message to print if the command fails\
exit_code) # If not zero, exit code to use if the command fails. (0 for "do not exit")
{
if (debug) printf("Debug: run_cmd(\"%s\") ", cmd)
if (system(cmd)) {
if (debug) print cmd ": failed."
if (error_message != "") print cmd ": " error_message > "/dev/stderr"
if (exit_code != 0) exit(exit_code)
return 1
}
if (debug) print " succeed."
return 0
}
# Run a command and return its output. If more than one result is returned by the system,
# only the first result will be returned. If all results are desired, pass the optional array
# as the fourth argument.
function cmd_result(\
cmd, # Command to execute\
# Optional arguments\
error_message,# Error message if the expected result does not return\
expect, # Expected result (one of the words: fail, string{s}, number{s}, or list) ("string" assumed is omitted.\
result, # If provided, the array of results.\
# Local variables\
i,\
temp,\
n,\
ok)
{
# Sanity checks
if (cmd=="") {
ok=1
print "cmd_result() was called with a null command." > "/dev/stderr"
}
if (expect == "") {
expect="string"
}
else {
expect=tolower(expect)
}
if (!((expect ~= /^number[s]?$/) || (expect ~= /^string[s]?/)") || (expect == "fail") || (expect == "list"))) {
ok=1
print "cmd_result(" cmd", "\" error_message "\", \"" expect ": Must be \"fail\", \"number[s]\", \"string[s]\", or \"list\"" >"/dev/stderr"
}
if (ok) return 2
#
n = 0
if (debug) printf("Debug: cmd_result(\"%s\") =", cmd)
result[1]=""
while (cmd | getline temp) {
if (debug) printf("\t\"%s\"\n", temp)
++n
result[n] = temp
}
close(cmd)
do {
if ( expect == "fail" ) {
if ( result[1] ) {
if (error_message != "") {
print cmd ": " error_message > "/dev/stderr"
}
else {
print cmd did not fail." > "/dev/stderr"
}
}
break
}
if ((n > 1) && (expect !="list") && (expect !~ /s$/)) {
print cmd ": Warning: " n " values were returned, not one."
break
}
if (expect ~ /^number/) {
for (i=1; i<=n;++i) {
result[i] = 0 + result[i]
}
break
}
else {
break
}
} while (1)
temp=result[1]
return temp
}
Well seeing it is not reliant on the index in the for loop, yes just run it prior to the loop and set it the same way once inside.
I must say I am not sure why you bother though as you could just do it all in awk. Also, You do realise that once you have read the whole file, ie once in END part of awk, that
your result value you have retrieved earlier is just the NR value??
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.