I'm using GDAL to get information about a satellite image. The stdout
looks like the following:
$ gdalinfo B02.jp2
Driver: JPEG2000/JPEG-2000 part 1 (ISO/IEC 15444-1)
Files: B02.jp2
B02.jp2.aux.xml
Size is 10980, 10980
Coordinate System is:
PROJCS["WGS 84 / UTM zone 15N",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AXIS["Latitude",NORTH],
AXIS["Longitude",EAST],
AUTHORITY["EPSG","4326"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-93],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
AUTHORITY["EPSG","32615"]]
Origin = (600000.000000000000000,5400000.000000000000000)
Pixel Size = (10.000000000000000,-10.000000000000000)
...
When I try to pull out a single line from stdout
, it yields an error:
$ gdalinfo B02.jp2 | grep Origin
maximum number of samples exceeded (120560400 > 67108864)
error: cannot decode code stream
Origin = (600000.000000000000000,5400000.000000000000000)
How can I extract information from the output (e.g. Origin
) and assign it to a variable?
You can use GNU grep
with its PCRE
capabilities enabled by -P
flag and store only the matching word within braces()
gdalinfo B02.jp2 2>/dev/null | grep -oP 'Origin = \(\K[^\)]+'
600000.000000000000000,5400000.000000000000000
The 2>/dev/null
is for suppressing error messages from your command. To store it in a variable just do,
myOriginInfo="$(gdalinfo B02.jp2 2>/dev/null | grep -oP 'Origin = \(\K[^\)]+')"
printf "%s\n" "$myOriginInfo"
Since you don't have GNU grep
installed, you can use this POSIX
compatible awk
expression to achieve your result,
awk 'BEGIN{FS="[()]"}/Origin/{print $2}' file
600000.000000000000000,5400000.000000000000000
and) in variable as
myOriginInfo="$(gdalinfo B02.jp2 2>/dev/null | awk 'BEGIN{FS="[()]"}/Origin/{print $2}')"