简体   繁体   中英

Print awk output on the same line

How do I print awk output on the same line.

I am using this to print the resolution of my monitor:

$ xrandr | awk '/\*/{print $1}'

When I connect a second monitor I get output on two lines,

$ xrandr | awk '/\*/{print $1}'

1920x1200
1600x1050

I would like to have the output on the same line. So it will look like this:

Monitor sizes: 1920x1200 1600x1050 

How would I do this the simplest way?

You can append xargs to your command, eg

$ xrandr | awk '/\*/{print $1}' | xargs

Here is alternative command using grep and xargs :

$ grep -o '[0-9]\+x[0-9]\+' <(xrandr) | xargs
1440x878 2880x1800 1440x900 2560x1600 2048x1280 1024x768 800x600 640x480 1680x1050 1280x800 1440x878

Or using echo :

$ echo Monitor sizes: $(grep -o '[0-9]\+x[0-9]\+' <(xrandr))
Monitor sizes: 1440x878 2880x1800

I have found this solution that is similar to what anubhava posted.

    $ xrandr | awk '/\*/{printf $1" "}'
    1920x1200 1600x1050

It is nice to know paste :

xrandr | awk '/\*/{print $1}' | paste - -

You can also iuse echo :

echo $(xrandr | awk '/\*/ {print $1}' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM