DEV Community

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

Posted on

2

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)

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay