Henry Navarro
Henry Navarro

Reputation: 953

How to take specific values in bash?

I have a list of dates non consecutive that looks like

data_list="20170410 20170411 20170412 20170414 20170415 20170416 20170417  20170419 20170420  20170422 20170423 24 20170425 20170516 20170517 20170518 20170519 20170520"

I convert it to array data_list=($data_list)

And then I want the dates between two specific dates saved in a variable. That is, I want the dates between 20170414 and 20170517.

Something like:

new_variable=x[between 20170414 and 20170517]
echo $new_variable
20170414 20170415 20170416 20170417  20170419 20170420  20170422 20170423 24 20170425 20170516 20170517

Do you know how to do it? I am a beginner in bash, so sorry if I am asking a Naive question.

Upvotes: 2

Views: 60

Answers (4)

jhnc
jhnc

Reputation: 16662

Passing arrays around like this probably isn't very efficient so you might decide to use a global variable (although that has its own issues).

#!/bin/bash

data_list="20170410 20170411 20170412 20170414 20170415 20170416
           20170417 20170419 20170420 20170422 20170423 24
           20170425 20170516 20170517 20170518 20170519 20170520"
data_list=( $data_list )

# usage: between lo hi list
between(){
    lo=$1; shift
    hi=$1; shift
    while [[ $# -gt 0 ]]; do
        if [[ $lo < $1 && $1 < $hi ]]; then
            echo $1
        fi
    done
}

new_variable=$(between 20170414 20170517 "${data_list[@]}")

echo $new_variable

Upvotes: 0

Socowi
Socowi

Reputation: 27215

The following bash function will print out numbers between a lower and upper bound (both inclusive):

between() {
  min=$1
  max=$2
  shift 2
  for date; do
    (( date >= min && date <= max )) && echo "$date"
  done | paste -sd' '
}

Your date format allows us to interpret dates as numbers. You can use the function as

between 20170414 20170517 "${arrayOfDates[@]}"

which will print

20170414 20170415 20170416 20170417 20170419 20170420 20170422 20170423 20170425 20170516 20170517

for your example data.

Upvotes: 0

choroba
choroba

Reputation: 241828

If the dates are already sorted, you can use loops to find the indices of the start and end of the range, than use parameter expansion to extract them:

#!/bin/bash
data_string='20170410 20170411 20170412 20170414 20170415 20170416 20170417  20170419 20170420  20170422 20170423 24 20170425 20170516 20170517 20170518 20170519 20170520'
data=($data_string)

for (( i=0 ; data[i] < 20170414 ; i++ )) ; do : ; done
for (( j=${#data[@]}-1; data[j] > 20170517 ; j-- )) ; do : ; done

echo ${data[@]:i:j-i+1}

Upvotes: 0

James Brown
James Brown

Reputation: 37404

Cheating with some awk:

$ new_list=$(echo $data_list | awk 'BEGIN{RS=ORS=" "}$0>=20170414 && $0<=20170517')
$ echo $new_list
20170414 20170415 20170416 20170417 20170419 20170420 20170422 20170423 20170425 20170516 20170517

Explained a bit:

echo $data_list |              # outputing the variable to awk 
awk 'BEGIN{
    RS=ORS=" "                 # separate records by a single space
}
$0>=20170414 && $0<=20170517'  # output lines between given values

Upvotes: 1

Related Questions