Loop question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop question
# 1  
Old 04-04-2005
Loop question

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:

Code:
#!/bin/sh

FILE="somefile.txt"
text_line=""

what kind of loop would use here?

# 2  
Old 04-04-2005
while read text_line; do

echo $text_line;

do < $FILE

IT will read line by line from $FILE variable stored filename.
# 3  
Old 04-04-2005
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
# 4  
Old 04-04-2005
thanks for that
# 5  
Old 04-04-2005
12 Ways To Parse A File

Enjoy...
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

# 6  
Old 04-04-2005
simple fileread and print it to screen

FILENAME=$1
IFS="
"
for LINE in $FILENAME; do
echo "$LINE"
done
# 7  
Old 04-05-2005
wow! great stuff...
thanks google. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question about for loop

Hi, I have code like below disk_list=$(ls /root/file1) for disk in $disk_list do pvcreate $i done I know what pvcreate command does, but I do not understand what this $i do here. can someone please explain. (2 Replies)
Discussion started by: stew
2 Replies

2. UNIX for Dummies Questions & Answers

Loop question

Hi, I would really appreciate some help with manipulating a file. Here is my input file: SNP_1 : 1 SNP_11 : 1 SNP_2 -0.12 1 SNP_12 -0.3472 1 SNP_3 -0.708 1 SNP_13 0.5037 1 SNP_4 1.492 0.5 SNP_14 -0.0685 1 SNP_5 1.27 0.5 SNP_15 0.547 ... (3 Replies)
Discussion started by: zajtat
3 Replies

3. Shell Programming and Scripting

While loop Question

Hi, I have a requirement where I have to check for 10 files in Unix location. If all of the files present in that directory then i have execute another process. can you help me resolving this issue. sample location /home/usr abc.txt cde.txt aaaa.txt lll.txt ooo.txt if all the... (3 Replies)
Discussion started by: manasvi24
3 Replies

4. Shell Programming and Scripting

For loop question

I have two files. In file one, there are many columns, but only two of interest to me. Column 1 contains a list of individuals, defined by an ID number. Column 10 contains the diagnosis that each individual has (I am a physician). All together, there are 3000 lines in this file, one line per... (2 Replies)
Discussion started by: awc228
2 Replies

5. Shell Programming and Scripting

For loop question

Hi, I'm trying to put together a small script that will read a txt file that contains a list of two columns. Each column is the name of a folder.. e.g. AIX Server1 AIX Server2 AIX Server3 $ for i in `cat /opt/apacheprod/scripts/input/copy_list.txt` do PLATFORMVAR=`awk ' { print $1 } '... (7 Replies)
Discussion started by: Jazmania
7 Replies

6. Shell Programming and Scripting

For Loop Question

I am struggling with the for loop. I have a file name heros.txt and I would like to go through a list in file where.txt and see if I can find the name from where inside heros. One of the problems that I am having is I dont understand how to setup the for loop to find the list to search.:wall: ... (6 Replies)
Discussion started by: captaindoogles
6 Replies

7. Shell Programming and Scripting

loop question

hey guys what im trying to do is do a simple script that will ask for a password and on the 5th time it says access denied if the right password is still not entered this is what i have so far can anyone help me im not good with scripting thanks in advance #!/bin/bash secretname=secret... (2 Replies)
Discussion started by: randallrivy11
2 Replies

8. Shell Programming and Scripting

For Loop Question

I'm improving the way an existing script handles arrays, but the results aren't what I had in mind: e="Too many consecutive errors... System is probably unstable!" e="Cancelable Timer Wait Failed!" for errcd in ${e} do echo ${errcd} done The for loop interprets the spaces... (2 Replies)
Discussion started by: ironhalo
2 Replies

9. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

10. Shell Programming and Scripting

loop question

HI ALL How to read first 10 line from a file using any method (5 Replies)
Discussion started by: aliahsan81
5 Replies
Login or Register to Ask a Question