1

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?

smci
  • 32,567
  • 20
  • 113
  • 146
sansSpoon
  • 2,115
  • 2
  • 24
  • 43

2 Answers2

2

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+"
bruchowski
  • 5,043
  • 7
  • 30
  • 46
  • 1
    I don't think GNU grep with ERE supports `\d` - mine doesn't. It requires `-P` to match a digit with `\d`. For BRE and ERE, there is always `[[:digit:]]`, though. – Benjamin W. Mar 11 '16 at 05:24
2

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."

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432