12 Ways to Parse a file

Tags
tutorials

 
Thread Tools Search this Thread
# 1  
Old 04-06-2005
12 Ways to Parse a file

A common thing in shell scripting. I came across this script that will be useful for people learning to write script.

Code:
#!/usr/bin/ksh
#
# SCRIPT: 12_ways_to_parse.ksh.ksh
#
#
# REV: 1.2.A
#
# PURPOSE:  This script shows the different ways of reading
#       a file line by line.  Again there is not just one way
#       to read a file line by line and some are faster than
#       others and some are more intuitive than others.
#
# REV LIST:
#
#       03/15/2002 - Randy Michael
#       Set each of the while loops up as functions and the timing
#       of each function to see which one is the fastest.
#
#######################################################################
#
#       NOTE: To output the timing to a file use the following syntax:
#
#          12_ways_to_parse.ksh file_to_process  > output_file_name 2>&1
#
#       The actaul timing data is sent to standard error, file 
#       descriptor (2), and the function name header is sent
#       to standard output, file descriptor (1).
#
#######################################################################
#
# set -n  # Uncomment to check command syntax without any execution
# set -x  # Uncomment to debug this script
#

FILENAME="$1"
TIMEFILE="/tmp/loopfile.out"
>$TIMEFILE
THIS_SCRIPT=$(basename $0)

######################################
function usage
{
echo "\nUSAGE: $THIS_SCRIPT  file_to_process\n"
echo "OR - To send the output to a file use: "
echo "\n$THIS_SCRIPT  file_to_process  > output_file_name 2>&1 \n"
exit 1
}
######################################
function while_read_LINE
{
cat $FILENAME | while read LINE
do
        echo "$LINE"
        :
done
}
######################################
function while_read_LINE_bottom 
{
while read LINE
do
        echo "$LINE"
        :

done < $FILENAME
}
######################################
function while_line_LINE_bottom
{
while line LINE
do
        echo $LINE
        :
done < $FILENAME
}
######################################
function cat_while_LINE_line  
{
cat $FILENAME | while LINE=`line`
do
        echo "$LINE"
        :
done
}
######################################
function while_line_LINE
{
cat $FILENAME | while line LINE
do
        echo "$LINE"
        :
done
}
######################################
function while_LINE_line_bottom 
{
while LINE=`line`
do
        echo "$LINE"
        :

done < $FILENAME 
}
######################################
function while_LINE_line_cmdsub2 
{
cat $FILENAME | while LINE=$(line)
do
        echo "$LINE"
        :
done
}
######################################
function while_LINE_line_bottom_cmdsub2 
{
while LINE=$(line)
do
        echo "$LINE"
        :

done < $FILENAME 
}
######################################
function while_read_LINE_FD 
{
exec 3<&0
exec 0< $FILENAME
while read LINE
do
        echo "$LINE"
        :
done
exec 0<&3
}
######################################
function while_LINE_line_FD 
{
exec 3<&0
exec 0< $FILENAME
while LINE=`line`
do
        echo "$LINE"
        :
done
exec 0<&3
}
######################################
function while_LINE_line_cmdsub2_FD
{
exec 3<&0
exec 0< $FILENAME
while LINE=$(line)
do
        print "$LINE"
        :
done
exec 0<&3
}
######################################
function while_line_LINE_FD 
{
exec 3<&0
exec 0< $FILENAME

while line LINE
do
        echo "$LINE"
        :
done

exec 0<&3
}
######################################
########### START OF MAIN ############
######################################

# Test the Input

# Looking for exactly one parameter
(( $# == 1 )) || usage

# Does the file exist as a regular file?
[[ -f $1 ]] || usage

echo "\nStarting File Processing of each Method\n"

echo "Method 1:"
echo "\nfunction while_read_LINE\n" >> $TIMEFILE
echo "function while_read_LINE"
time while_read_LINE >> $TIMEFILE
echo "\nMethod 2:"
echo "\nfunction while_read_LINE_bottom\n" >> $TIMEFILE
echo "function while_read_LINE_bottom"
time while_read_LINE_bottom >> $TIMEFILE
echo "\nMethod 3:"
echo "\nfunction while_line_LINE_bottom\n" >> $TIMEFILE
echo "function while_line_LINE_bottom"
time while_line_LINE_bottom >> $TIMEFILE
echo "\nMethod 4:"
echo "\nfunction cat_while_LINE_line\n" >> $TIMEFILE
echo "function cat_while_LINE_line"
time cat_while_LINE_line >> $TIMEFILE
echo "\nMethod 5:"
echo "\nfunction while_line_LINE\n" >> $TIMEFILE
echo "function while_line_LINE"
time while_line_LINE >> $TIMEFILE
echo "\nMethod 6:"
echo "\nfunction while_LINE_line_bottom\n" >> $TIMEFILE
echo "function while_LINE_line_bottom"
time while_LINE_line_bottom >> $TIMEFILE
echo "\nMethod 7:"
echo "\nfunction while_LINE_line_cmdsub2\n" >> $TIMEFILE
echo "function while_LINE_line_cmdsub2"
time while_LINE_line_cmdsub2 >> $TIMEFILE
echo "\nMethod 8:"
echo "\nfunction while_LINE_line_bottom_cmdsub2\n" >> $TIMEFILE
echo "function while_LINE_line_bottom_cmdsub2"
time while_LINE_line_bottom_cmdsub2 >> $TIMEFILE
echo "\nMethod 9:"
echo "\nfunction while_read_LINE_FD\n" >> $TIMEFILE
echo "function while_read_LINE_FD"
time while_read_LINE_FD >> $TIMEFILE
echo "\nMethod 10:"
echo "\nfunction while_LINE_line_FD\n" >> $TIMEFILE
echo "function while_LINE_line_FD"
time while_LINE_line_FD >> $TIMEFILE
echo "\nMethod 11:"
echo "\nfunction while_LINE_line_cmdsub2_FD\n" >> $TIMEFILE
echo "function while_LINE_line_cmdsub2_FD"
time while_LINE_line_cmdsub2_FD >> $TIMEFILE
echo "\nMethod 12:"
echo "\nfunction while_line_LINE_FD\n" >> $TIMEFILE
echo "function while_line_LINE_FD"
time while_line_LINE_FD >> $TIMEFILE


|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Different ways to check a file SkySmart Shell Programming and Scripting 1 01-05-2018 04:12 AM
How to parse a file? aoussenko Shell Programming and Scripting 3 10-03-2016 09:34 PM
Parse A Log File Ariean Shell Programming and Scripting 13 05-22-2014 12:30 PM
How to parse a binary file? Ophiuchus UNIX for Advanced & Expert Users 15 05-23-2013 03:34 AM
Parse XML file leemalloy Programming 1 05-23-2013 01:24 AM
parse fasta file to tabular file yifangt Shell Programming and Scripting 6 12-25-2011 06:58 PM
Parse a file with awk? tulf210 Shell Programming and Scripting 2 08-09-2011 01:24 PM
Parse a file greycells Shell Programming and Scripting 7 03-28-2011 12:43 PM
need to parse the jil file into an excel file ramky79 Shell Programming and Scripting 7 10-19-2010 03:13 PM
Parse file from remote server to calculate count of string existence in that file poweroflinux Shell Programming and Scripting 6 08-31-2010 03:57 AM
ways to open a file venkatesht UNIX for Dummies Questions & Answers 3 02-17-2009 06:54 AM
How to parse through a file and based on condition form another output file sivasu.india UNIX for Advanced & Expert Users 6 02-28-2008 04:59 AM
Need help to parse the file navsharan Shell Programming and Scripting 3 01-17-2008 03:58 PM
Parse file sbasetty Shell Programming and Scripting 5 03-27-2007 02:27 PM
using getopt to parse a file coolguyshail Shell Programming and Scripting 1 06-08-2005 07:58 AM