简体   繁体   中英

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM