Saving Awk Output to Variable

Saving awk output to variable

#!/bin/bash

variable=`ps -ef | grep "port 10 -" | grep -v "grep port 10 -" | awk '{printf $12}'`
echo $variable

Notice that there's no space after the equal sign.

You can also use $() which allows nesting and is readable.

store awk output in variable

It should be like this:

#! /bin/bash
File1=$1
for (( j=1; j<=3; j++ ))
{
output=$(awk -F ';' 'NR=='$j' {print $3}' "${File1}")
echo ${output}
}

It working well on my CentOS.

How to pass AWK output into variable?

Use command substitution to capture the output of a process.

#!/bin/sh

VAR="$(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop/logs/Default.log)"
echo "$VAR"

some general advice with regards to shell scripting:

  • (almost) always quote every variable reference.
  • never put spaces around the equals sign in variable assignment.

Save awk output into variable

You should not have a space between Name= and $(awk. That turns it into a temporary variable assignment for one command, similar to the difference between:

name=bob    # sets name to bob
name= bob # sets name to nothing while running bob

In other words, use:

prompt> Name=$(awk -F '.' 'FNR == 2 {print $NF}' file_name.txt)
prompt> echo $Name
Sam

Assign AWK result to variable

The following works correctly on bash:

 a=$(echo '111 222 33' | awk '{print $3;}' )
echo $a # result is "33"

Another option would be to convert the string to an array:

 a="111 222 333"
b=($a)

echo ${b[2]} # returns 333

SH: save awk command output in variables

with bash

#!/usr/bin/env bash

disk=
cpu_usage=
memory_usage=

while true; do
read -r disk
read -r cpu_usage
read -r memory_usage
break
done < <(awk -f tst.awk)

printf '%s\n' "$disk" "$cpu_usage" "$memory_usage"

where tst.awk

BEGIN {
while("df -hP " | getline) {
if ( $NF == "/" ) {
printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
}
}
while( getline < "/proc/loadavg" ) {
printf "CPU Load: %.2f\n", $(NF-2)
}

while( "free -m"| getline) {
if( $0 ~ /Mem:/) {
printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
}
}
}

Instead of a separate script for awk, one can add the awk code inside the Process substitution.

done < <(awk 'BEGIN {
while("df -hP " | getline) {
if ( $NF == "/" ) {
printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
}
}
while( getline < "/proc/loadavg" ) {
printf "CPU Load: %.2f\n", $(NF-2)
}

while( "free -m"| getline) {
if( $0 ~ /Mem:/) {
printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
}
}
}'
)

Without the while loop, using { } for command grouping.


{
read -r disk_usage
read -r cpu_usage
read -r memory_usage
} < <( awk 'BEGIN {
while("df -hP " | getline) {
if ( $NF == "/" ) {
printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5
}
}
while( getline < "/proc/loadavg" ) {
printf "CPU Load: %.2f\n", $(NF-2)
}
while( "free -m"| getline) {
if( $0 ~ /Mem:/) {
printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2
}
}
}'
)

printf '%s\n' "$disk_usage" "$cpu_usage" "$memory_usage"


Related Topics



Leave a reply



Submit