Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-28-2008, 04:32 PM   #1
LQ Newbie
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
How to read in line-by-line from a file and store them in several variables

Hi all,

I need to read line-by-line from multiple files of the same format and store each line to a variable so that I can do arithmatic with those multiple variables in bash. For example,
Let's say I have two text files of this format
1 2
3 4
5 6 7 and

9 10
11 12
13 14 15

I want to read each line from the two files and take average of each line so that resulting numbers will be

5 6
7 8
9 10 11.

How would I do this most effectively? I'm thinking using arrays will somehow be helpful but haven't been able to successfully implement it.

Thanks for any comments.

Last edited by ncjlee; 09-28-2008 at 04:33 PM.
Old 09-28-2008, 06:20 PM   #2
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

uhm, this question is not so easy to answer to - I would prefer to implement it using a "real" programming language like perl, but you asked for a bash solution. It took me a while to get a running script - and it's not easy to understand! You have been warned ;-)

I assume, that the number of files, the number of lines within each file and the number of fields within each line are not pref-defined, but all files have the same structure (all files have exactly n lines and the line with number x has exactly m fields in each file)? I don't check that in my script.

So let's look at my bash script. The problem is, that you need a 3 dimension array (file, line, fields), but the bash offers only 1 dimension arrays. So I produced different arrays with dynamically created names. My way to do this is eval. I inserted some output on STDERR to show, how the script works. Feel free to redirect it to /dev/null, if you only want to see the result.

jan@jack:~/tmp/multi_files> cat data1
1 2
3 4
5 6 7
jan@jack:~/tmp/multi_files> cat data2
9 10
11 12
13 14 15
jan@jack:~/tmp/multi_files> cat data3
18 20
21 22
23 24 25
jan@jack:~/tmp/multi_files> cat
#! /bin/bash


for file_name in $*; do
  echo $file_name "-->" $file_no >&2
  while read line; do
    echo -e "\tline_no=$line_no line=($line)" >&2
    eval "declare -a f_arr_${file_no}_${line_no}"
    eval "f_arr_${file_no}_${line_no}=($line)"
    eval echo '${f_arr_'${file_no}'_'${line_no}'[*]}' >&2
    line_no=$((line_no + 1))
  done <$file_name
  file_no=$((file_no + 1))

line_no=$((line_no - 1))
file_no=$((file_no - 1))
echo >&2

for l_no in `seq 0 $line_no`; do
  num_fields=`eval echo '${#f_arr_0_'${l_no}'[*]}'`
  echo l_no=$l_no num_fields=$num_fields >&2
  for (( i=0; i < $num_fields; i++ )); do
    for f_no in `seq 0 $file_no`; do
      eval echo l_no=$l_no f_no=$f_no i=$i '${f_arr_'${f_no}'_'${l_no}'['$i']}' >&2
      val=`eval echo '${f_arr_'${f_no}'_'${l_no}'['$i']}'`
      sum=$((sum + $val))
    avg=$((sum / ($file_no + 1)))
    echo avg = $sum / $((file_no + 1)) >&2
    output="$output $avg"
  echo $output

exit 0
jan@jack:~/tmp/multi_files> ./ data* 2>/dev/null
9 10
11 12
13 14 15
I didn't comment anything - it's too late here in germany and I have to go to work in some hours. If you don't understand some parts of my script - ask. I'll answer later this day.



array, file, read

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration