DEV Community

Cover image for How to parse a key-value pair list with AWK
MoniqueLive
MoniqueLive

Posted on

How to parse a key-value pair list with AWK

A simple example, using env as input:

$ env | grep EDITOR
EDITOR=vim
Enter fullscreen mode Exit fullscreen mode
$ env | awk -F= '{a[$1]=$2} END {print(a["EDITOR"])}'
vim
Enter fullscreen mode Exit fullscreen mode

It's way easier to just do echo $EDITOR though.

But sometimes it's not so easy:

$ xdotool getactivewindow getwindowgeometry --shell | awk -F= '{a[$1]=$2} END {print(a["WIDTH"])}'
1430
Enter fullscreen mode Exit fullscreen mode

If you want to get the current window's width.

_

= M =

Top comments (0)