Trim after nth occurrence with loop

Hello,
Below command trims right after the nth occurrence of a string.
When I try in while loop , it is not working.

In Terminal

IFS=/ ; read -ra val < Textfile ; echo "${val[*]:0:3}"

It gives only one line:

sunday/monday/tuesday

Textfile:

sunday/monday/tuesday/wednesday/thursday
august/feb/jan/april/
london/texas/paris/michigan/ontario

Expected output:

sunday/monday/tuesday
august/feb/jan
london/texas/paris

Script:

#!/bin/bash

while read -ra val
do
echo "${val[*]:0:3}"
done < Textfile

I could not solve this simple question.
Please let me know my fault.

Many thanks
Boris

Try:

#!/bin/bash

oldIFS=$IFS
IFS=/
while read -ra val
do
  echo "${val[*]:0:3}"
done < Textfile
IFS=$oldIFS

--
Another way:

cut -d/ -f-3 Textfile
1 Like

Dear Scrutinizer,
Many thanks, I am not familiar with field seperator command but both works nice.

Kind regards
Boris