ropsi
ropsi

Reputation: 141

Extract values by their position

Using the histogram function of gdalinfo, I am saving the frequency of pixel values in a textfile. My objective is to extract the first and last value of the histogram and save them in a variable. Since I am new the Linux environment, I don't know how to use grep to select the numbers by their position.

13691313 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24599

Upvotes: 0

Views: 39

Answers (3)

Benjamin W.
Benjamin W.

Reputation: 52112

Extracting the first and last field with awk:

awk '{ print $1, $NF }' filename

Or, if your histogram is stored in a string, you can use a here-string:

awk '{ print $1, $NF }' <<< "$stringname"

If you'd like to assign them separately to shell variables:

$ var1="$(awk '{ print $1 }' filename)"
$ var2="$(awk '{ print $NF }' filename)"

Upvotes: 2

BeniBela
BeniBela

Reputation: 16907

You can use ^ and $ to anchor the grep expression at the beginning or end:

echo "your string" | grep -oE '(^[0-9]+)|([0-9]+$)'

Upvotes: 0

kometen
kometen

Reputation: 7772

If the string does not change, ie. same amount of space you can use

echo "your string" | cut -d " " -f 1,256

And cut should show

13691313 24599

Upvotes: 0

Related Questions