hi,
how would i go about making a loop which gets each line from a single text file, set it to a variable and then print it to screen?
thanks
eg:
#!/bin/sh
FILE="somefile.txt"
text_line=""
what kind of loop would use here?
hi,
how would i go about making a loop which gets each line from a single text file, set it to a variable and then print it to screen?
thanks
eg:
#!/bin/sh
FILE="somefile.txt"
text_line=""
what kind of loop would use here?
while read text_line; do
echo $text_line;
do < $FILE
IT will read line by line from $FILE variable stored filename.
How abt this........
#!/bin/sh
FILE="somefile.txt"
COUNT=0
#Storing in a array
while read LINE_FROM_FILE
do
TEXT_LINE[$COUNT]=$LINE_FROM_FILE
COUNT=`expr $COUNT + 1`
done < $FILE
I=0;
#Printing the array
while [ $COUNT -ne 0]
do
echo ${TEXT_LINE[$I]}
I=`expr $I + 1`
COUNT=`expr $COUNT - 1`
done
Thanks
Om
thanks for that
Enjoy...
#!/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
simple fileread and print it to screen
FILENAME=$1
IFS="
"
for LINE in $FILENAME; do
echo "$LINE"
done
wow! great stuff...
thanks google.
just another quick question, how would i incorporate this with the "grep" command say I only wanted to grab lines that contained a certain phrase and store each line to a variable?
#!/bin/sh
while read text_line; do
echo $text_line;
done < $FILE
#! /bin/ksh
grep "hello there" $file | \
while read text_line
do
echo $text_line;
done
exit 0
got another question, say i wanted to do the same thing but i wanted to start reading from a file starting from a line that contains a certain string, how can i do so?
#!/bin/sh
SOMESTRING="some string"
while read text_line; do
echo $text_line;
done < $FILE
how can i start reading the file from the line which has $SOMESTRING
#!/bin/sh
SOMESTRING="some string"
sed "/${SOMESTRING}/,$p" ${FILE} | while read text_line; do
echo $text_line;
done
thanks for that, just curious what does the ",$p" do?
also i tried combining "sed" with "grep" in this example;
#!/bin/sh
SOMESTRING="some string"
sed "/${SOMESTRING}/,$p" ${FILE} | grep "hello there" $FILE | while read text_line; do
echo $text_line;
done
in other words start reading the file starting from the line containing SOMESTRING and get the lines which contain "hello there" after that. Is this legal?
i tried it and get this error:
sed -e expression #1, char 88:Unexpected `,'
ooops - sorry - forgot something:
#!/bin/sh
SOMESTRING="some string"
sed "/${SOMESTRING}/,\${/hello there/p;}" ${FILE} | while read text_line; do
echo $text_line;
done
i just tried using that and i get this error
sed: -e expression #1, char 58: Unknown command: `4'
strange indeed ...
quote exactly what you have tried as I've modified my last post - it works fine under Solaris' stock 'sed'.
ok i fixed the error,
this is exactly what i tried...
#!/bin/sh
FILE="somefile"
SOMESTRING="some string"
sed "/${SOMESTRING}/,\${/hello there/p;}" ${FILE} | while read text_line; do
echo $text_line;
done
the contents of "somefile" which i made is:
line 1
line 2
line 3
some string
blah
hello there
blah
hello there
this is what it prints out
line 1
line 2
line 3
some string
blah
hello there
hello there
blah
hello there
hello there
which is not right ...
sorry again - I thought I'd posted what I'd tried here locally, but.. here it's:
#!/bin/sh
FILE="somefile"
SOMESTRING="some string"
sed -n "/${SOMESTRING}/,\${/hello there/p;}" ${FILE} | while read text_line; do
echo $text_line;
done
wow, works like a charm now, thanks for that