Reading tab (\t) separated text from file in shell

3.2k Views Asked by At

my text file looks like this

1   get a   06-05-2000
2   get b   06-05-2001
3   get c   06-05-2002
4   get d   06-05-1442
5   get e   06-05-1998
6   get f   06-05-1909

I want to read what is after the first \t (tab) and store that in varA and what is after \t (the other tab) and store that in varB

using SHELL

1

There are 1 best solutions below

0
On BEST ANSWER

In Bash you can do this:

#!/bin/bash
declare -a varA varB
while IFS=$'\t' read -r num first second;do
varA+=("$first")
varB+=("$second")
done <file
echo ${varA[1]} ${varB[1]} 

You can access each element of varA array using index ${varA[$index]} or all of them at once with ${varA[@]}.