Issue
With openwrt I usually use UCI to get data in variables for some scripts. Something like that:
uci get /etc/config/network.lan.data
Unfortunately I did not find something similar for Linux in general but I believe that awk or sed can do it without needing numerous outputs.
In general, the first string is only a parameter to find the second
input:
...
config 'wan'
data 'a1 a2 a3'
something 'words'
config 'lan'
info 'words'
data 'b1 b2 b3'
something 'words'
config 'net'
something 'words'
info 'words'
data 'c1 c2'
...
Output:
b1 b2 b3
--- Edit: --
I believe that with the input of this form would be broader the functionality of the script:
input:
...
config something 'wan'
something some_data 'a1 a2 a3'
something 'words'
config something 'lan'
something some_info 'words'
something some_data 'b1 b2 b3'
something 'words'
config something 'net'
something 'words'
something some_info 'words'
something some_data 'c1 c2'
...
Attention to requests, here a better explanation:
1 - find line with 'lan' (or maybe with config.*.'lan')
2 - If found, search the following lines for the first line with a word ending data (maybe *.data)
3 - Print the content between ' ' of this line
Output:
b1 b2 b3
What is the best solution?
Grateful for the attention!
Solution
You can use something like:
awk -v C="lan" -v F="data" '$1=="config" { gsub(/^[\47"]|[\47"]$/,"",$2); conf=$2; next } conf==C && $1==F { $1=""; gsub(/^ *[\47"]|[\47"]$/, ""); print }' YOURFILE
Input:
config 'wan'
data 'a1 a2 a3'
something 'words'
config 'lan'
info 'words'
data 'b1 b2 b3'
something 'words'
config 'net'
something 'words'
info 'words'
data 'c1 c2'
Output:
b1 b2 b3
Slightly different approach for the updated question:
awk -v C="lan" -v F="data" 'BEGIN { FS="\47"; REG=".*"F"[ \t]*" } $1~"config[ \t]" { conf=$2 } conf==C && $1~REG { print $2 }' YOURFILE
Input:
config something 'wan'
something some_data 'a1 a2 a3'
something 'words'
config something 'lan'
something some_info 'words'
something some_data 'b1 b2 b3'
something 'words'
config something 'net'
something 'words'
something some_info 'words'
something some_data 'c1 c2'
Output:
b1 b2 b3
Answered By - Andriy Makukha Answer Checked By - Katrina (WPSolving Volunteer)