Searching content of a variable using awk

so i'm running a variation of the following command a few times in my script:

echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {print $2}' | tr '\n' '|'

unfortunately i cant paste the content of the variable TOTALRunning into this thread for security reasons.

what i want to do is combine the three piped commands into one.

also, I want the "tr" part of the command to run only IF there are MORE than ONE lines are found matching whatever the pattern is that is stored in the variable PROCSEARCH.

any ideas?

Hello SkySmart,

Could you please try following, though couldn't test it as sample Input_file was not there.

echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q ORS $2:$2} END{gsub("\n","|",Q);print Q}'  
 OR
echo "${TOTALRunning}" | awk -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q "|" $2:$2} END{print Q}' 

Thanks,
R. Singh

1 Like
echo "${TOTALRunning}" | awk -F"[. ]" -vP="${PROCSEARCH}" /$0~P/ {printf("%s|",$2)}END{print "\n"}'
1 Like

It would be good if you could supply a short made up example snippet of the values being used, just to make sure.

i would like to combine everything into one awk code. is something like this possible:

awk  -v val="${TOTALRunning}" -F"[. ]" '/'"${PROCSEARCH}"'/ {Q=Q?Q ORS $2:$2} END{gsub("\n","|",Q);print Q}'

There is no inputfile. The content of the variable "TOTALRunning" is produced by a different script.

Hello SkySmart,

I give second to Zaxxon like at least sample Input_file(with a raw/dummy data, which looks similar to actual one) could be pasted to post.

Thanks,
R. Singh

Maybe you can't show us the actual value assigned to TOTALRunning , but surely you could show us some representative strings that would emulate its value and:

  1. tell us what operating system you're using,
  2. what shell you're using,
  3. and what output you hope would be produced from those sample assignments.

We need to know:

  1. whether the string that has been assigned starts with a - or contains any \ characters (that will significantly affect the output produced by echo in some shells on some operating systems),
  2. whether we are processing a single line of input containing \n escape sequences or multiple lines of input separated by actual <newline> characters,
  3. whether the value assigned to that variable contains complete lines or the last line assigned to that variable is missing the line terminating <newline> character, and
  4. whether you really want all <newline> characters in the output to be translated to pipe symbols if more than one line is selected for output or you want all but the last line of output to be separated by pipe symbols and the last line of output to be terminated by a <newline> character no matter how many lines are selected for output.

If you aren't willing to give us a clear idea of what your input looks like and what output you're trying to produce, I have no confidence that anything I might suggest has any chance of doing what you want done.

If an external script is creating the contents of the variable TOTALRunning , why confuse all of us with this variable? Why not just pipe the output of that external script to your awk script? (But, we still need to know the format of the output that external script produces and a clear specification of the output you want to produce.)

the content of TOTALRunning looks something like this:

TOTALRunning='HOST-RESOURCES-MIB::hrSWRunPath.31050 = STRING: "/usr/bin/mod_gearman_worker"
HOST-RESOURCES-MIB::hrSWRunPath.31213 = STRING: "/usr/bin/mod_gearman_worker"
HOST-RESOURCES-MIB::hrSWRunPath.31214 = STRING: "/usr/bin/mod_gearman_worker"
HOST-RESOURCES-MIB::hrSWRunPath.31237 = STRING: "/usr/bin/mod_gearman_worker"
'

Sorry for the inconvenience guys.

The thing is, i have two variables i have to work with. The first is the TOTALRunning, which i have pasted. The combined command i'm looking for searches the TOTALRunning variable and gets a particular number or numbers, based on the amount of lines found containing the content of PROCSEARCH.

Then, it uses the number(s) that were retrieved from the parsing the TOTALRunning variable, to search another variable, which contains this:

TOTALRunPerfMem='HOST-RESOURCES-MIB::hrSWRunPerfMem.30694 = INTEGER: 3008 KBytes
HOST-RESOURCES-MIB::hrSWRunPerfMem.31213 = INTEGER: 3020 KBytes
HOST-RESOURCES-MIB::hrSWRunPerfMem.31311 = INTEGER: 3020 KBytes
HOST-RESOURCES-MIB::hrSWRunPerfMem.31312 = INTEGER: 3020 KBytes'

the command i use on the second variable this:

echo "${TOTALRunPerfMem}" | awk '/HOST-RESOURCES-MIB::hrSWRunPerfMem.('${NOIPID}') / && !/No Such Instance currently exists at this OID/ {print $4}'

if all of this can be done combined into one command, that would be great

WHY are you making all of us guess WHAT you want, and how? After minutes of reasoning and analysing your posts, I infer you want a list of sort of PIDs from the first variable, separated by an alternation operator for the regex match in the second string, of which you then print a Kbyte count, here 3008 or 3020.
Wouldn't it be easier for all of us if you describe your problem in such a simple manner?