Wednesday, October 26, 2022

[SOLVED] How to get values in a line while looping line by line in a file (shell script)

Issue

I have a file which looks like this (file.txt)

{"key":"AJGUIGIDH568","rule":squid:111-some_random_text_here
{"key":"TJHJHJHDH568","rule":squid:111-some_random_text_here
{"key":"YUUUIGIDH566","rule":squid:111-some_random_text_here
{"key":"HJHHIGIDH568","rule":squid:111-some_random_text_here
{"key":"ATYUGUIDH556","rule":squid:111-some_random_text_here
{"key":"QfgUIGIDH568","rule":squid:111-some_random_text_here

I want to loop trough this line by line an extract the key values.

so the result should be like ,

AJGUIGIDH568
AJGUIGIDH568
YUUUIGIDH566
HJHHIGIDH568
ATYUGUIDH556
QfgUIGIDH568

So I wrote a code like this to loop line by line and extract the value between {"key":" and ","rule": because key values is in between these 2 patterns.

while read p; do 
  echo $p | sed -n "/{"key":"/,/","rule":,/p"
done < file.txt

But this is not working. can someone help me to figure out me this. Thanks in advance.


Solution

Your sample input is almost valid json. You could tweak it to make it valid and then extract the values with jq with something like:

sed -e 's/squid/"squid/' -e 's/$/"}/' file.txt | jq -r .key

Or, if your actual input really is valid json, then just use jq:

jq -r .key file.txt

If the "random-txt" may include double quotes, making it difficult to massage the input to make it valid json, perhaps you want something like:

awk '{print $4}' FS='"' file.txt 

or

sed -n '/{"key":"\([^"]*\).*/s//\1/p' file.txt

or

while IFS=\" read open_brace key colon val _; do echo "$val"; done < file.txt


Answered By - William Pursell
Answer Checked By - Senaida (WPSolving Volunteer)