LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   looping line by line and saving output result on diff files with different names (https://www.linuxquestions.org/questions/linux-newbie-8/looping-line-by-line-and-saving-output-result-on-diff-files-with-different-names-4175415068/)

iconig 07-05-2012 10:31 AM

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

montel 07-05-2012 10:56 AM

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.

iconig 07-05-2012 11:29 AM

It does not save beta for each of the lines on a separate file. It only gives one file named beta

montel 07-05-2012 12:09 PM

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.

iconig 07-05-2012 01:18 PM

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

montel 07-05-2012 01:32 PM

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

Alchemikos 07-05-2012 05:44 PM

Hello
I think that's you're looking for:
Code:

i=0; while read line; do  echo "$line" >> "beta$i";((i++));done <odon
Cheers

chrism01 07-05-2012 05:53 PM

[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/


All times are GMT -5. The time now is 11:34 PM.