SFIA Helmi
SFIA Helmi

Reputation: 1

how to extract variable from file.txt unix

How to extract "oid" from a file with the following structure using shell scripting?

file name :variable
file body:
"title":"script1"
"oid":"jjjnerfjeffrefef6"
"user":"xxxx"

I would like to only extract the oid value (jjjnerfjeffrefef6).

Upvotes: 0

Views: 34

Answers (1)

dawg
dawg

Reputation: 103714

Given:

$ echo "$s"
"title":"script1" "oid":"jjjnerfjeffrefef6" "user":"xxxx"

You can use sed with a regex:

$ echo "$s" | sed -ne 's/^.*"oid":"\([^"]*\).*$/\1/p' 
jjjnerfjeffrefef6

Which would also work for a file:

$ sed -ne 's/^.*"oid":"\([^"]*\).*$/\1/p' file.txt

Upvotes: 1

Related Questions