user8514011
user8514011

Reputation: 13

Remove first n "words" from string variable in Bash

I want to remove the first 4 words from my string variable "DATES".

Does someone have a simple solution for this?

Here my example:

DATES="31 May 2021 10:22:01 30 May 2021 10:23:01 29 May 2021 10:24:01"
WC=$(echo $DATES | wc -w)                                                                                          
DATE_COUNT=$(( $WC / 4 - 1 ))                                                                                     

for i in {0..$DATE_COUNT}                                                                                          
do                                                                                                                 
   YEAR=$(echo $DATES | awk '{print $3}')                                                                         
   MONTH=$(echo $DATES | awk '{print $2}')                                                                        
   MONTH=$( date --date="$(printf "01 %s" $MONTH)" +"%m")                                                         
   DAY=$(echo $DATES | awk '{print $1}')                                                                          
   TIME=$(echo $DATES | awk '{print $4}' | sed 's/://g')                                                          
   DATE_ARRAY[$i]="$YEAR$MONTH$DAY$TIME"                                                                          
    
   #Remove first 4 words from string                                                                               
done  

Upvotes: 1

Views: 1317

Answers (3)

Jetchisel
Jetchisel

Reputation: 7791

Maybe use read ?

DATES="31 May 2021 10:22:01 30 May 2021 10:23:01 29 May 2021 10:24:01"
read -ra dates <<< "$DATES"; echo "${dates[@]:4}"

Or just store the data in an array directly.

DATES=(31 May 2021 10:22:01 30 May 2021 10:23:01 29 May 2021 10:24:01)

echo "${DATES[@]:4}"

To get the total words/elements like with wc -c

echo "${#DATES[*]}"

Upvotes: 2

James Brown
James Brown

Reputation: 37404

Using bash regex operator =~:

$ [[ $DATES =~ ^(([^ ]+ +){4})(.*) ]] && echo ${BASH_REMATCH[3]}
30 May 2021 10:23:01 29 May 2021 10:24:01

Upvotes: 2

Miguel
Miguel

Reputation: 2219

Use cut.

DATES="31 May 2021 10:22:01 30 May 2021 10:23:01 29 May 2021 10:24:01"
echo $DATES | cut -d' ' -f 5-

Output:

30 May 2021 10:23:01 29 May 2021 10:24:01

You can even use it for a cleaner solution than awk, like this:

YEAR=$(echo $DATES | cut -d' ' -f 3) 

General version to remove n first words

remove_n_first_words(){
    echo $2 | cut -d' ' -f $(($1+1))-
}
remove_n_first_words 4 "$DATES"

Upvotes: 4

Related Questions