Extract key value in shell script - linux

I want to extract the value of pt & userId in a variable in shell script.
I've below value set in a varibale which comes dynamically & need to extract pt & userId
{"pt":"PT-24fesxPGJIHOe714iaMV-13dd3872781-sin_pos","userId":"66254363666003"}
Can any one tell me how to extract these values in shell script?
Note: I don't want to use JSON parser just to parse 2 strings.
Thanks!

This string appears to be a JSON string and its better to use dedicated JSPN parser like underscore for parsing this text. Once underscore cli is installed you can do:
# extract pt
echo $jsonStr | underscore select '.pt'
# extract userId
echo $jsonStr | underscore select '.userId'
Though not recommended but if you really want to parse it in shell you can use awk like this:
awk -F, '$1 ~ "pt" {gsub(/[^:]+:"|"/, "", $1); print $1}
$2 ~ "userId" {gsub(/[^:]+:"|"}/, "", $2); print $2}'
OR even simpler:
awk -F'"' '{print $4 "\n" $8}'
Output:
PT-24fesxPGJIHOe714iaMV-13dd3872781-sin_pos
66254363666003

You can use following script
var={"pt":"PT-24fesxPGJIHOe714iaMV-13dd3872781-sin_pos","userId":"66254363666003"}
echo $var
pt=`echo $var|cut -d, -f1|awk -F':' '{ print $2 }'`
echo $pt
userId=`echo $var|cut -d, -f2|awk -F':' '{ print $2 }'|tr -d '}'`
echo $userId
In this script stores values in 2 variables "pt" and "userId", which you can use.

Related

How can I change only one number from a certain line in a csv format file using linux comand?

I have a csv file that looks like this:
ID,Name,Salary,Email
23,John,3000,john#gmail.com
15,Mike,3200,mike#gmail.com
16,Tom,3500,tom#gmail.com
How can i change the salary of an employee using the ID as an identifier?
I tried
echo "Id: "
read ID
echo "New salary: "
read s
awk -F "," '{ if ($1 = $ID) $3 = $s } END{print $0}' employees.csv
Use == for comparison, not =.
Shell variables aren't expanded inside single quotes. You should use the -v1 option to assign the shell variables to awk variables.
read -p "Id: " id
read -p "New salaray: " s
awk -F ',' -v id="$id" -v salary="$s" '$1 == id { $3 = salary; print }' employees.csv > new_employees.csv

String manipulation in shell in a single line

I am using the below script
list=kmakalas#gmail.com;kmakalas#gmail.com;kmakalas#gmail.com;
for the above I wanted extract to
r=kmakalas,r=kmakalas,r=kmakalas
for that I used the below shell manipulations
rev_list="r=${list//#gmail.com;/r=}
the above gave me
r=kmakalas,r=kmakalas,r=kmakalas,r=
to get r=kmakalas,r=kmakalas,r=kmakalas
I used rev2="${rev_list%,r=}"
Is there any possibility to do in a single line command
Using GNU awk:
awk -F '[=;]' '{ for(i=2;i<NF;i++) { split($i,map,"#");printf i==(NF-1)?"r="map[1]:"r="map[1]"," } printf "\n" }' <<< "$list"
Explanation:
awk -F '[=;]' '{ # Set the field delimiter to "=" or ";"
for(i=2;i<NF;i++) {
split($i,map,"#"); # Loop through each field and split the field/address into an array map using "#" as the delimiter
printf i==(NF-1)?"r="map[1]:"r="map[1]"," # Print "r=" along with the "#" prefix
}
printf "\n"
}' <<< "$list"

AWK comparing two variables that are strings [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 3 years ago.
I want to get a user intput at the begining af the script (as an argument) and then compare it to a string using awk !
I'm using -v to add the variable into the awk command(by the way the input is STRING)
But for some reason it won't show the expected output !
And i know that the problem is on the variable that i inserted into the awk command , because if i put instead of the variable the string that i want to find, the specific one that i know that is inside the file , it will find it and print the result i want.
I will show you code !
awk -v x=$a -F '[:,]' ' { if ($1 == $a ) print $5 }' /etc/passwd
i have also tried:
awk -v x="$a" -F '[:,]' ' { if ($1 == "$a" ) print "$5" }' /etc/passwd
But nothing. I cannot find the solution.
A clarification here. I have made the correct seperation and i know the file what it includes!
And here an example that works without the variable
awk -v x=$a -F '[:,]' ' { if ($1 == "psz" ) print $5 }' /etc/passwd
psz is the string that i have set before the awk command at the a variable ( a="psz" ) like that.
And it is what i know that is inside the /etc/passwd at the first field ($1) !!
You should use instead of this:
awk -v x=$a -F '[:,]' ' { if ($1 == $a ) print $5 }' /etc/passwd
this:
awk -v x=$a -F '[:,]' ' { if ($1 == x ) print $5 }' /etc/passwd
You define x as a variable and this should be used in awk

Unix AWK command - multiple character as a single delimiter

I am trying to use two data pipeline, || , as delimiter in AWK command. But I am unable to do it. I have a file in which I have to consider two data pipeline as delimiter, just like considering TAB or COMMA as delimiter.
Just tell awk to interpret the | literally with []:
awk -F'[|][|]' ...
Example:
ยป echo "1 || 2" | awk -F'[|][|]' '{ print $2 }'
2

bash, extract string from text file with space delimiter

I have a text files with a line like this in them:
MC exp. sig-250-0 events & $0.98 \pm 0.15$ & $3.57 \pm 0.23$ \\
sig-250-0 is something that can change from file to file (but I always know what it is for each file). There are lines before and above this, but the string "MC exp. sig-250-0 events" is unique in the file.
For a particular file, is there a good way to extract the second number 3.57 in the above example using bash?
use awk for this:
awk '/MC exp. sig-250-0/ {print $10}' your.txt
Note that this will print: $3.57 - with the leading $, if you don't like this, pipe the output to tr:
awk '/MC exp. sig-250-0/ {print $10}' your.txt | tr -d '$'
In comments you wrote that you need to call it in a script like this:
while read p ; do
echo $p,awk '/MC exp. sig-$p/ {print $10}' filename | tr -d '$'
done < grid.txt
Note that you need a sub shell $() for the awk pipe. Like this:
echo "$p",$(awk '/MC exp. sig-$p/ {print $10}' filename | tr -d '$')
If you want to pass a shell variable to the awk pattern use the following syntax:
awk -v p="MC exp. sig-$p" '/p/ {print $10}' a.txt | tr -d '$'
More lines would've been nice but I guess you would like to have a simple use awk.
awk '{print $N}' $file
If you don't tell awk what kind of field-separator it has to use it will use just a space ' '. Now you just have to count how many fields you have got to get your field you want to get. In your case it would be 10.
awk '{print $10}' file.txt
$3.57
Don't want the $?
Pipe your awk result to cut:
awk '{print $10}' foo | cut -d $ -f2
-d will use the $ als field-separator and -f will select the second field.
If you know you always have the same number of fields, then
#!/bin/bash
file=$1
key=$2
while read -ra f; do
if [[ "${f[0]} ${f[1]} ${f[2]} ${f[3]}" == "MC exp. $key events" ]]; then
echo ${f[9]}
fi
done < "$file"

Resources