LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-05-2012, 11:31 AM   #1
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Rep: Reputation: Disabled
looping line by line and saving output result on diff files with different names


I have been trying this program for a long time. I am trying to read a file named "odon" line by line; read the first line, send it to do a command saved in a file "perm", once the first line has finished going through the content of the file perm, the result is saved with the number of the line. Once that is done, the script reads the second line and repeats the process until all the lines have been read, sent to the other file and the output saved. This is what I mean:
12 1 1
2 1 1
3 1 1
92 1 1
read line 12 1 1, send it into file "perm" which contains a command, then the output is saved as beta. The next file as beta1, the next file as beta2 etc. The content of perm will output "beta" when it is read in the right way. I used only one 12 1 1 and it gave me beta, but to read each line and save with different names is proving difficult.
This is what I have done so far:
#!/bin/sh
for i in 'cat odon'
do echo $i | ./perm $i > $i
cp beta beta.$i
done
This does not seem to work as I want. I would appreciate your contribution. Thanks
 
Old 07-05-2012, 11:56 AM   #2
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
Look into using a while loop.

Code:
while read line ; do

done < "odon"
Ill test out the rest of your code in a minute, but this should do what you need.
 
Old 07-05-2012, 12:29 PM   #3
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
It does not save beta for each of the lines on a separate file. It only gives one file named beta
 
Old 07-05-2012, 01:09 PM   #4
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
So how come you are using a separate bash script for one command? Could you just do this?

Code:
i=0
while read line ; do 
     num=`echo $line | cut -d' ' -f1`
     echo $num >> beta$i
     i=`expr $i + 1`
done < "odon"
Thats just an example, I am not sure what your command is you want to run on it, but this will create separate beta files for each time the loop is executed.
 
1 members found this post helpful.
Old 07-05-2012, 02:18 PM   #5
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
its is not creating different files. I am trying to get a different beta for each line when it passes through perm. what are f1 and -d
 
Old 07-05-2012, 02:32 PM   #6
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
That was just an example. The code I put in my last post will output different beta files (beta0, beta1, beta2...etc) with the first number before a space in the file "odon". I was unsure of why you were sending the data to a different script instead of just putting it all in this one. What I typed up was just an example of cutting the first number.

According to my script, it will take the input of odon:
12 1 1
2 1 1
3 1 1
92 1 1

And output into beta0:
12

output to beta1:
2

output to beta2:
3

etc...
 
Old 07-05-2012, 06:44 PM   #7
Alchemikos
Member
 
Registered: Jun 2012
Location: Porto Alegre-Brazil
Distribution: Slackware- 14, Debian Wheezy, Ubuntu Studio, Tails
Posts: 88

Rep: Reputation: 6
Hello
I think that's you're looking for:
Code:
i=0; while read line; do  echo "$line" >> "beta$i";((i++));done <odon
Cheers
 
1 members found this post helpful.
Old 07-05-2012, 06:53 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
[quote]
what are f1 and -d
[/code]
http://linux.die.net/man/1/cut

See also
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Diff 2 files (not line by line) Leath Linux - Newbie 4 05-22-2012 07:19 PM
How to compare two files line by line and print the line which is same. nancypriyanjali Programming 9 05-30-2011 11:04 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 01:38 PM
Grep's line numbers parsed into one line of output. judgex Programming 8 08-14-2006 05:22 AM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 08:45 PM


All times are GMT -5. The time now is 06:30 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration