LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-14-2008, 07:04 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
Looping in script help needed


Hi all,


I have 2 files which has only ,same "n" number of lines which are all numbers. I need to subtract them to the alternate line of the other file.

Code:
Say, file1 has as below:

20
40
60
80
100
Code:
file2 has as below:

10
30
50
70
90
I need a loop to subtract the with their alternate files as below:

Code:
20-10=10
40-30=10
60-50=10
This is just an example.I need someone to help me to frame the loop for this.

Thanks
 
Old 08-14-2008, 07:50 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
To read one file line by line:

while read line; do <something>; done < filename

To loop through two files at the same time, I think you can use redirection to switch between the files.

This link gives some clues:
http://www.unix.com/tips-tutorials/1...arse-file.html

I found this on Google using "read line from file"
 
Old 08-14-2008, 07:52 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
In shell scripts you can read from more than one file by using re-direction into a while loop, and then read from a specific file handle using the -u option to read, e.g.

Code:
#!/bin/bash

while true; do
        read -u 2 from_file1
        read -u 3 from_file2

        # stop if we have an empty file from either file
        [ "$from_file1" = "" ] && break
        [ "$from_file2" = "" ] && break

        echo "$from_file1 - $from_file2 = $(($from_file1 - $from_file2))"
done 2<first_file 3<second_file
 
Old 08-14-2008, 08:12 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
So you're assigning the files to file descriptors 2 and 3.....but I thought fd 2 was reserved for STDERROR.
???
 
Old 08-14-2008, 08:17 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
oop, you're right Use 3 and 4
 
Old 08-18-2008, 05:17 AM   #6
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thank you all,

For your kind updates on this Post. And Sorry for my delayed response as I went for the week-end.Its Working fine.
I need the Output only and the way you guys explain here , made me to learn a lot of ways

Thanks Again
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script help needed: patilvidya Linux - Software 2 08-29-2007 08:53 AM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM
Looping a bash script SoulGrind Linux - Newbie 5 09-22-2006 06:03 PM
little script help needed abisko00 Programming 7 07-11-2005 08:37 AM
shell script looping ust Linux - General 2 01-19-2005 04:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:07 AM.

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