Bash awk: parsing variable string into another variable? [duplicate] - bash

This question already has answers here:
Linux bash: Multiple variable assignment
(6 answers)
Closed 4 years ago.
I would like to extract all the values contained in $line and put them in variables var1, var2,... varn. I did use this previously to extract the vars from file (in.txt)
var1=$(awk '{print $1}' < in.txt)
var2=$(awk '{print $2}' < in.txt)
....
varn=$(awk '{print $n}' < in.txt)
How should I change my awk call so as to use $line instead of in.txt?
I tried these for example
echo $line | var2=$(awk '{print $2}')
or
var2=$(echo $line | awk '{print $2}')
but without success...
========== DETAIL==============
----- calling file:
.....
name=Matrix
line=$(sed -n '/^\[T\]/ {n;p}' in.txt)
echo 'line: ' $line
L1=$(./procline_matrix_vars.sh $line 30 $name)
echo 'L1: ' $L1
------- rocline_matrix_vars.sh:
#!/bin/bash
line=$1
choice=$2
var1=$(echo $line | awk '{print $1}')
var2=$(echo $line | awk '{print $2}')
var3=$(echo $line | awk '{print $3}')
var4=$(echo $line | awk '{print $4}')
if [ $choice == 30 ]; then
L1=$(printf '\n\n\n%s = [ %s %s %s %s \n' "$3" "$var1" "$var2" "$var3" "$var4")
fi
echo "${L1%.}"

a possible way:
line="aaa bbb ccc"
var=( $line )
echo "${var[1]}"
echo "my array has ${#var[#]} elements"
output
bbb
my array has 3 elements
maybe shortcut
var=( $( awk '{print $1, $2, $10}' file ) )

Related

How to grab fields in inverted commas

I have a text file which contains the following lines:
"user","password_last_changed","expires_in"
"jeffrey","2021-09-21 12:54:26","90 days"
"root","2021-09-21 11:06:57","0 days"
How can I grab two fields jeffrey and 90 days from inverted commas and save in a variable.
If awk is an option, you could save an array and then save the elements as individual variables.
$ IFS="\"" read -ra var <<< $(awk -F, '/jeffrey/{ print $1, $NF }' input_file)
$ $ var2="${var[3]}"
$ echo "$var2"
90 days
$ var1="${var[1]}"
$ echo "$var1"
jeffrey
while read -r line; do # read in line by line
name=$(echo $line | awk -F, ' { print $1} ' | sed 's/"//g') # grap first col and strip "
expire=$(echo $line | awk -F, ' { print $3} '| sed 's/"//g') # grap third col and strip "
echo "$name" "$expire" # do your business
done < yourfile.txt
IFS=","
arr=( $(cat txt | head -2 | tail -1 | cut -d, -f 1,3 | tr -d '"') )
echo "${arr[0]}"
echo "${arr[1]}"
The result is into an array, you can access to the elements by index.
May be this below method will help you using
sed and awk command
#!/bin/sh
username=$(sed -n '/jeffrey/p' demo.txt | awk -F',' '{print $1}')
echo "$username"
expires_in=$(sed -n '/jeffrey/p' demo.txt | awk -F',' '{print $3}')
echo "$expires_in"
Output :
jeffrey
90 days
Note :
This above method will work if their is only distinct username
As far i know username are not duplicate

How to remove the usage of temp file and read data from the command itself

I have a shell script and I need help to make it efficient. I am using temp files to store and read the data, but I need to read the data in memory.
It collects metrics from the Postgres database using a command and fetches the metrics. My current script fetches the metrics to a temp file, then reads from it.
I want to stop using temp files and use memory instead.
The script works, I just need help to automate more and get rid of reading data from temp files.
List item
INPUT=`mktemp`
#/usr/pgsql-9.5/bin/pgbench -c1 -j1 -t 1000 -S man > $INPUT
TESTTIME=15 #seconds
echo "Waiting $TESTTIME seconds..."
/usr/pgsql-9.5/bin/pgbench -c1 -j1 -T $TESTTIME -r man > $INPUT
OLDIFS=$IFS
IFS=" "
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
tps=`cat $INPUT |awk '/^tps/ {print $3}' |awk -F'.' '{print $1}' |head -n1`
update_l=`cat $INPUT |awk '/UPDATE/ {print $1}' |tail -n1`
select_l=`cat $INPUT |awk '/SELECT/ {print $1}' |tail -n1`
insert_l=`cat $INPUT |awk '/INSERT/ {print $1}' |tail -n1`
echo ${PLOTTER_PREFIX}.tps $tps kv
echo ${PLOTTER_PREFIX}.update_latency $update_l kv
echo ${PLOTTER_PREFIX}.select_latency $select_l kv
echo ${PLOTTER_PREFIX}.insert_latency $insert_l kv
#{ while read line; do
# # statsite_buildData ${PLOTTER_PREFIX}.latency average ${latency average} kv
# echo ${PLOTTER_PREFIX}.${line} kv
# done } < $INPUT
statsite_sendData
#echo $Test
IFS=$OLDIFS
rm -f $INPUT
You can capture the output of the command to a variable, like so:
output=$(/usr/pgsql-9.5/bin/pgbench -c1 -j1 -T $TESTTIME -r man)
Then just use echo instead of cat and substitute $INPUT with the variable name.
tps=`echo "$output" | awk '/^tps/ {print $3}' | awk -F'.' '{print $1}' |head -n1`
update_l=`echo "$output" | awk '/UPDATE/ {print $1}' | tail -n1`
...
I would also suggest using $() instead of surrounding commands with backticks. So the above would become:
tps=$(echo "$output" | awk '/^tps/ {print $3}' | awk -F'.' '{print $1}' |head -n1)
update_l=$(echo "$output" | awk '/UPDATE/ {print $1}' | tail -n1)
...

Bash Shell Issue

currentDate="20160324"
headerDumpFile="header.txt"
#currentDate="$(date +ā€™%Y%m%dā€™)"
printf "Current date in dd/mm/yyyy format %s\n" $currentDate
contId=""
labelList="c12,playlist-play,play,pause,end,playlist-end,heartbeat,ns_st_cl"
params="corporate=abc&user=abc&password=abc&startdate=$currentDate&site=abc&extralabels=$labelList"
url="https://example.com/v1/start?$params"
a=1
while true
do
curl -D $headerDumpFile -v -k -H "Accept-Encoding:gzip" $url > $a.zip
contId= cat $headerDumpFile | grep "X-CS-Continuation-Id:" | awk '{print $NF}'
if [ "$contId" ];then
printf "Breaking the Loop.."
break;
fi
url="https://example.com/v1/start?$params&continuationId=${contId}"
a=$((a + 1))
echo $contId
echo $url
done
When i Do echo url its giving value of contId as blank but when i do echo $contId. Its printed correctly .Please suggest
Perhaps is it what you want to achieve:
contId=$(cat $headerDumpFile | grep "X-CS-Continuation-Id:" | awk '{print $NF}')
Or the simpler:
contId=$(awk '/X-CS-Continuation-Id:/ {print $NF}' $headerDumpFile)
Note that unlike what you were guessing, echo $contId isn't displaying anything in your code. What is displayed is the result of the bogus contId= cat $headerDumpFile | grep "X-CS-Continuation-Id:" | awk '{print $NF}' line.

Parsing in bash - extract content from brackets

The input to my bash script can be of the form [fec1::1]:80 or []:80. The second input implies that there's no IP address given. My bash script is to split the input into IP and port. With the said second input, the script should 'understand' that no IP was given.
The following logic seems to solve my problem, on the bash prompt:
$ ip=[]:78
$ echo $ip
[]:78
$ temp=(`echo $ip | awk -F'[][]' '{print $2}'`)
$ echo $temp
$
When I try to do the same thing from within a script, the result is different:
local_endpoint="$1"
printf 'local_endpoint: %s\n' "$local_endpoint"
IN="$local_endpoint"
local_ip=$(echo "$IN" | awk -F'[][]' '{print $2}')
if [ -z "$local_ip" ] ; then
local_ip=$(echo "$IN" | awk -F':' '{print $1}')
local_port=$(echo "$IN" | awk -F':' '{print $2}')
else
local_port=$(echo "$IN" | awk -F'[][]' '{print $3}' | awk -F':' '{print $2}')
fi
printf 'IP: %s\n' $local_ip
printf 'port: %d\n' $local_port
if [ -z "$local_port" -a -z "$local_ip" ] ; then
printf 'No port and IP was given\n'
elif [ -z "$local_ip" ] ; then
printf 'No IP was given\n'
elif [ -z "$local_port" ] ; then
printf 'No port was given\n'
fi
exit 2
Output:
# ./temp.sh []:829
local_endpoint: []:829
IP: []
port: 829
Any idea on what's happening? Also, why do I see the extra comma (,) at the end of the output?
Your script is missing quoting at many places and there are stray commas too in printf. This script should work:
local_endpoint="$1"
printf 'local_endpoint: %s\n' "$local_endpoint"
IN="$local_endpoint"
if [[ "$IN" == "["* ]] ; then
local_ip=$(echo "$IN" | awk -F'[][]' '{print $2}')
local_port=$(echo "$IN" | awk -F'[][]' '{print $3}' | awk -F':' '{print $2}')
else
local_ip=$(echo "$IN" | awk -F':' '{print $1}')
local_port=$(echo "$IN" | awk -F':' '{print $2}')
fi
printf 'IP: <%s>\n' "$local_ip"
printf 'port: <%d>\n' "$local_port"
if [ -z "$local_port" -a -z "$local_ip" ] ; then
printf 'No port and IP was given\n'
elif [ -z "$local_ip" ] ; then
printf 'No IP was given\n'
elif [ -z "$local_port" ] ; then
printf 'No port was given\n'
fi
exit 2
Process substitution is:
var=$(command ...)
not
var=(command ...)

bourne shell single-quote, doublequote & backquote question

/!bin/sh
if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
echo "howdy dody"
fi
echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'
First if-condition won't run, im guessing it's because of improper quotation, but i can't figure it out.
Thanks in advance for any help.
You can also use single quotes around the argument to the -F option as you have around other arguments:
if [ "`echo $desc $status | awk -F',' '{print $3}' | awk -F' ' '{print $1}' | sed '/^$/d'`" != "OK" ]; then
It is much easier to write your test if you wrap it in a function:
mytest()
{
echo "$1 $2" \
| awk -F"," -v arg3="$3" '{print arg3}' \
| awk -F" " -v arg1="$1" '{print arg1}' \
| sed '/^$/d'
}
This way, you can verify that it works correctly. Once you gained this confidence
if [ "$(mytest "$desc" "$status")" != "OK" ]; then
echo "howdy doody"
fi
or
if mytest "$desc" "$status" | grep -q -v '^OK$'; then
echo "howdy doody"
fi
If you're using Bash, I'd recommend $(...) instead of back-quotes. What error messages do you get? My guess is that the -F"," option to awk is not being quoted properly. Trying inserting \ to escape the quotation marks.
At first glance, you might want to try escaping some of the double quotes:
if [ "`echo $desc $status | awk -F"," '{print $3}' | awk -F" " '{print $1}' | sed '/^$/d'`" != "OK" ]; then
echo "howdy dody"
fi
to
if [ "`echo $desc $status | awk -F\",\" '{print $3}' | awk -F\" \" '{print $1}' | sed '/^$/d'`" != "OK" ]; then
echo "howdy doody"
fi
Escaping the double quotes is certainly a good idea, but it looks like the $3 and the $1 are intended to be interpreted by awk. They are being interpreted by your shell instead. You probably want to escape the '$'s. (It is possible that you have meaningful values for $1 and $3 in the shell, but not likely.)

Resources