Read text file line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read text file line by line
# 1  
Old 02-19-2011
Read text file line by line

Hi everyone,

I am writing a BASH shell script that I will use to process data files. I have a text file that contains the names of the files to be processed, for example:

Code:
cat filenames.txt 
file1
file2
file3
file4
file5

What I would like to do is set up a FOR loop within my script that will iterate for the # of file names in the text file. Each time through the loop I will assign the successive file name to a variable. In other words, the first time through the loop the variable FILENAME will take on the value 'file1', the second time 'file2', etc. However I am not sure how to do that. Any help is greatly appreciated!

Mike
# 2  
Old 02-19-2011
Simpler to do it with a while loop

Code:
while read LINE
do
    FILENAME="$LINE"
    # do something here with $FILENAME
done < filenames.txt

# 3  
Old 02-19-2011
Hi Aia,

Thank you very much. That works great and is exactly what I need.

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

How to read the first work of each line from text file?

Hi ppl. I want to read the first word of each line of a text file to a variable in a bash script. Can anyone help pls, thanks. (6 Replies)
Discussion started by: cannot
6 Replies

6. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

7. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

8. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

9. Shell Programming and Scripting

Script does not read the last line of text file

Hello, I have got a script that reads a text file, and have got three problems that I an struggling with. 1. The script does not read the last line in the text file 2. within the second 'elif' within the script I included a 'break' - the script runs successfully (except for the first... (2 Replies)
Discussion started by: jermaine4ever
2 Replies

10. Shell Programming and Scripting

Read from text file misses first line

Hi! I need to read in the first line from a text file (which will only ever have one line in it), so I tried this.... while read line do echo $line done < $file But this wasn't returning anything. So I tired a different file, which had multiple lines of text in it, and it returned... (2 Replies)
Discussion started by: davewg
2 Replies
Login or Register to Ask a Question