Get just the integer value of the image width from SIPS, without "pixelWidth: ..."

1k views Asked by At

When getting an image's pixelWidth using SIPS it outputs with a heading. e.g.

sips -g pixelWidth $images

returns

"  pixelWidth: 1920"

I'm trying to get the integer part only, with no luck:

sips -g pixelWidth $images | grep \d+$

Any Ideas?

2

There are 2 answers

1
bruchowski On BEST ANSWER

You want to use grep's -E (regex) and -o (capture matches) flags, this works for me:

sips -g pixelWidth menu_icon.png | grep -Eo "\d+"

Note if you have any numbers in your file path to the image(s), they will show up (since sips prints the file path too), so you might want to add grep pixelWidth before you grep out the number, like this:

sips -g pixelWidth menu_icon.png | grep pixelWidth | grep -Eo "\d+"
0
Mark Setchell On

Just an easy-to-read, robust alternative as you already have a working answer, you could use awk like this:

sips -g pixelWidth image.png | awk '/pixelWidth:/{print $2}'

That says... "on lines that contain the word 'pixelWidth:', please print the second field."