Reading a delimited file in Shell script

By | 24th August 2020

In the ETL world, it is always a need for processing the delimited files. While reading a delimited file can be done through all the scripts and programming languages, we will see how this can be done in a shell script using the cut command

Syntax

$(cut -d "|" -f1 <Delimited file> | tr -d '[:space:]')

cut -d “|” => This is to tell the command that the delimiter is “|” for this operation

-f1 <Delimited file> => This will read the first column from the delimited file

tr -d ‘[:space:]’ => This will delete the extra spaces at the end of the values

Let us see it with a real-time example. In the following example, we will see how to read the delimited content and assign it to variables. The sample file is “delimitedfile.txt”

delimitedfile.txt

$ cat delimitedfile.txt
1|Sankar|Sales|2020=08-01|Chennai

Let us see a shell script that reads and displays the above-delimited file.

filereader.sh

export sourcefile="delimitedfile.txt"
echo "Source file to read = $sourcefile"

echo "Program to read delimited file starts..."
export id=$(cut -d "|" -f1 ${sourcefile} | tr -d '[:space:]')
export name=$(cut -d "|" -f2 ${sourcefile} | tr -d '[:space:]')
export department=$(cut -d "|" -f3 ${sourcefile} | tr -d '[:space:]')
export joineddate=$(cut -d "|" -f4 ${sourcefile} | tr -d '[:space:]')
export city=$(cut -d "|" -f5 ${sourcefile} | tr -d '[:space:]')

echo ""
echo "ID                :       $id"
echo "NAME              :       $name"
echo "DEPT              :       $department"
echo "JOINED ON         :       $joineddate"
echo "CITY              :       $city"
echo ""

When you run the above script you will see the result as follows.

$ sh filreader.sh
Source file to read = delimitedfile.txt
Program to read delimited file starts...

ID		    :	1
NAME		:	Sankar
DEPT		:	Sales
JOINED ON	:	2020=08-01
CITY		:	Chennai

Also check “How to iterate string list using for loop in shell