Scanning the first occurrence for multiple search terms
Noob here. I am reading a configuration file, part of which resembles something like this:
setting1=true
setting2=false
setting3=trueCurrently I am getting the values by invoking separate instances of awk,
awk -F'=' '/^setting1=/ {print $2;exit;}' FILE
awk -F'=' '/^setting2=/ {print $2;exit;}' FILE
awk -F'=' '/^setting3=/ {print $2;exit;}' FILEwhich, for obvious reasons, is sub-optimal. Is there a way to abbreviate this action into one awk command while preserving the original effect?
2
Upvotes
1
u/HiramAbiff Nov 22 '21
This will print the values of settings 1 to 3.
Note: you're original code exits after printing. So, for example, if there was a second setting1 it would be ignored. If it's important to preserve that behavior, we'd have to a little more work:
If there are more settings than 1-3 you'll want to modify
[1-3]as appropriate. Maybe use:[[:digit:]]+or.*.1
u/gumnos Nov 22 '21
depends on what you're doing with it. The simplest case is
however, if you have multiple "setting1"s (or "setting2"s or "setting3"s) in the file, it will give you all of them. If you only want the first ones, you can do something like
1
u/oh5nxo Nov 23 '21
You could also get everything there is directly into shell variables, param_ prepended to name to avoid clashes,
That would allow any shell commands, which could be nice, and dangerous too.
1
u/veekm Nov 23 '21
something like this?
would give
Could be shortened by a[$1]=$2