Awk Print Row Instead of Column

cd .. || cd

June 18, 2022 · 1 mins · Robertus Chris

Table of Contents

Main Course

Let’s say we have an output from xrandr --listactivemonitor like this:

Monitors: 2
 0: +*eDP-1 1920/344x1080/194+1920+0  eDP-1
 1: +HDMI-2 1920/480x1080/270+0+0  HDMI-2

Now, we want to display only the second row which is this line:

 0: +*eDP-1 1920/344x1080/194+1920+0  eDP-1

We can do that using awk with this command:

xrandr --listactivemonitor | awk 'NR==2'

or using process substitution command, like this:

awk 'NR==2' <(xrandr --listactivemonitor)

If we also want to limit the row and the column, let’s say the second row and the third column which is result in this:

1920/344x1080/194+1920+0

We can do that with this command:

xrandr --listactivemonitor | awk 'NR==2 {print $3}'

or using process substitution command, like this:

awk 'NR==2 {print $3}' <(xrandr --listactivemonitor)

Now, if we want to display the second to last row, which is this line:

 0: +*eDP-1 1920/344x1080/194+1920+0  eDP-1
 1: +HDMI-2 1920/480x1080/270+0+0  HDMI-2

We can do that with this command:

xrandr --listactivemonitor | awk 'NR>=2'

or using process substitution command, like this:

awk 'NR>=2' <(xrandr --listactivemonitor)

Alright, that’s all. Thanks for reading and happy shell scripting!

Reference