Bash – Read Text Files

Background

On my Linux box, I will like to generate textural data files on a scheduled basis.

 

Read Data File

Code

Bash


#!/bin/bash

declare numberofCmdLineArgs=0
declare filename=""
declare line=""
declare lineNumber=0
declare dataArray
declare attributed=""
declare quote=""

#Get Number of Command Line Arguments
numberofCmdLineArgs="$#"

#If Number of Command Line Arguments is not equal to 1
#indicate that you must pass in filename
if [ "$numberofCmdLineArgs" -ne 1 ]; then

    echo "You must pass in the filename as an argument"

    exit 1

fi

#get file name as first argument passed to shell
filename=$1

#If Filename is not indicated, please say so
if [ ! -f "$filename" ] 
then
  
  printf "File ( %s ) does not exist! \n" $filename

  exit 2  

fi

#set line number as zero (0)
lineNumber=0

#Set Delimeter using IFS ( Special variable )
IFS='~'

#read records
while read -r line
do

    #increment line number
    lineNumber=$((lineNumber+1))

    #read line into an array
    read -a dataArray <<< "$line"

    #get array size
    arrayLen_="${#dataArray[@]}"

    if [ $arrayLen_ -gt 0 ]
    then

        #get author
		attributed=${dataArray[0]}

    fi

    if [ $arrayLen_ -gt 1 ] 
    then

        #get quote
		quote=${dataArray[1]}


    fi

    #display author and quote
    if [ ! -z "$attributed" ]
    then

         echo "${attributed}"

    fi      

    #display quote
    if [ ! -z "$quote" ]
    then

        echo "${quote}"
    
    fi

    #add line separator
    if [ $arrayLen_ -gt 0 ]
    then

		echo
        echo 

    fi

#while more records to read
done < $1

#Unset IFS ( Special variable )
unset IFS

Invoke


sh ./readTextfile.sh literature.txt

Output

Output – Image

Output – Text


Alan J. Perlis
The programmer must seek both perfections of part and adequacy of collection.


Alan J. Perlis
Thus, programs must be written for people to read, and only incidentally for machines to execute.


Keith Devens
Premature abstraction is an equally grievous sin as premature optimization.


Steven Den Beste
Sometimes a man with too broad a perspective reveals himself as having no real perspective at all. A man who tries too hard to see every side may be a man who is trying to avoid choosing any side. A man who tries too hard to seek a deeper truth may be trying to hide from the truth he already knows.  That is not a sign of intellectual sophistication and "great thinking". It is a demonstration of moral degeneracy and cowardice.

Code Sharing

Bash

DanielAdeniji/readTextfile.sh
Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s