Search code examples
bashmacosterminalexiv2

Output specific line for exiv2?


Using exiv2, you can output image information in the terminal. However, it outputs several lines of information:

exiv2 -ps ~/filelocation/filename.jpg

Outputs something like this:

File name       : ~/filelocation/filename.jpg
File size       : 12345 Bytes
MIME type       : image/jpeg
Image size      : 800 x 600
~/filelocation/filename.jpg: No Exif data found in the file

How do I command the terminal to only output the image size data from this?

What I really want is this:

exiv2 -ps ~/filelocation/filename.jpg [some command here]

Output:

800 x 600

Solution

  • Try this -

    exiv2 -ps ~/filelocation/filename.jpg |
      sed -n '/Image size/{ s/^.*: //; p; }'
    

    sed's -n suppresses default output.

    /pattern/ matches line by line.

    {...} wraps a script of actions to take on lines that match.

    s/^.*: //; strips the leading string.

    p; prints the value.