looping line by line and saving output result on diff files with different names
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.