1

I have a txt file with this lines:

2017-06-15 Take the car for inspection to change the wheels.mkd
2018-03-17 Crear un entorno virtual con Docker y xfce.mkd
2018-02-25 Envíar vídeo de explicación de configuración email de tunegocioenlanube a María.mkd
2018-03-08 crear curso tu formula emocional +tunegocio.mkd

I want to put in a array of bash the year, month and day:

year=( "2017","2018" )
month=( "03","06","02" )
day=( "08","15","17","25" )

Thanks for all.

4 Answers4

1

You can to solve this using regex. See awk or sed

But, if regex it's so complex for you, you can solve the problem with a simpler form, using cut command.

cut is a command what allows to get a specific section of a line (similar to split in other languages).

You only have to set the delimiter with -d option, and select the field with -f

In your case, you can use, in each line of the file:

date=$(echo $line | cut -d " " -f 1)
year=$(echo $date | cut -d "-" -f 1)
month=$(echo $date | cut -d "-" -f 2)
day=$(echo $date | cut -d "-" -f 3)

With this, you can get the fields that you needs. Then, you can save this on a array or whatever you needs

To add elements to a existing array, you can read this post: https://unix.stackexchange.com/questions/328882/how-to-add-remove-an-element-to-from-the-array-in-bash

And, to read file line to line, this: Read a file line by line assigning the value to a variable

AlmuHS
  • 387
  • 6
  • 13
1

If the order isn't important, then you could use something like this:

#!/bin/bash

declare -A years months days # declare associative arrays

while read -r date file; do
    IFS=- read -r year month day <<<"$date" # split date on -

    # set keys in associative arrays
    years[$year]=  
    months[$month]=
    days[$day]=
done < file

# use keys to make arrays of values
year=( "${!years[@]}" )
month=( "${!months[@]}" )
day=( "${!days[@]}" )

If you want to sort the values in the output array, then you can change the final assignments to e.g.:

mapfile -d '' day < <(printf '%s\0' "${!days[@]}" | sort -zn)

This prints each of the keys in days followed by a null byte and sorts them in numerical order, saving the result into the array day.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
1

You could do:

year=($(grep -Po '^\d+[^-]' InputFile.txt | uniq))
month=($(grep -Po '(?<=-)[0-9]+(?=-)' InputFile.txt | sort -u))
day=($(grep -Po '(?<=-)[0-9]+(?= )' InputFile.txt) | sort -u))

Sample output echo ${day[@]}:

08 15 17 25
builder-7000
  • 7,131
  • 3
  • 19
  • 43
0
while read Line
     do
         echo $Line
         var1=`echo "$Line" | awk 'BEGIN{FS=" "} {print $1}' | awk 'BEGIN{FS="-"} {print $1}'`
         var2=`echo "$Line" | awk 'BEGIN{FS=" "} {print $1}' | awk 'BEGIN{FS="-"} {print $2}'`
         var3=`echo "$Line" | awk 'BEGIN{FS=" "} {print $1}' | awk 'BEGIN{FS="-"} {print $3}'`
         year+=("$var1")
     month+=("$var2")
     day+=("$var3")
done < array.txt

The above bash code will read the lines one by one from array.txt, extract the values and store it in an array.To print all the values in the array, you can use the following:

printf '%s\n' "${year[@]}"
printf '%s\n' "${month[@]}"
printf '%s\n' "${day[@]}"
hduser
  • 7
  • 5