[SOLVED] How to save each line from textfile as new file?
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.
Hi Guys,
just started with Linux and I'm trying to create new files for each line of my input file. So I have file with 637 lines of data:
2 4 6
2 8 5
3 0 5
etc
and want to create a new file from each line. With
cat name.txt | awk '{ line = $0
print line
}'
I nicely see all lines, but what rests is to save each line separately into new file. I tried While read line command in combination with output >> $.txt, but didn't work well....
cat file | while read LINE; do echo $LINE > $LINE.txt; done
the filename is pretty useless but that will achieve what you wanted.
Thanks it worked! First I had error saying ambiguous redirect but after putting variable in quotes it turned out well.
Regarding the filename the output is indeed quite useless.... So how could I refer the filename to the first column, and not including this first column (=name) into the contents of the new files? Thanks!
Regarding the filename the output is indeed quite useless.... So how could I refer the filename to the first column, and not including this first column (=name) into the contents of the new files? Thanks!
Not sure about the filename part, since from your example there are two lines whose first columns have the same value. Are they supposed to be written in the same file? In this case:
This will exclude the first column (field) from the content of the files. If you want to put all the lines in different files (despite the value of the first field) you can use a counter so that file something_001.txt contains the first line, something_002.txt contain the second line and so on. For example:
Not sure about the filename part, since from your example there are two lines whose first columns have the same value. Are they supposed to be written in the same file? In this case:
This will exclude the first column (field) from the content of the files. If you want to put all the lines in different files (despite the value of the first field) you can use a counter so that file something_001.txt contains the first line, something_002.txt contain the second line and so on. For example:
Ok, the good thing is that it works for most of the files. However, it gives problems with longer file names which contains letter+text such as M25162 and M36384. In that case it will also treat the last numbers as columns. Any idea to solve this? Thanks!
However, it gives problems with longer file names which contains letter+text such as M25162 and M36384. In that case it will also treat the last numbers as columns.
Please explain your last assertion. What numbers are treated as columns? Please, show us an example of real input (one of the not working cases) and the desired output.
then for files M26 and M632 it indeed uses contents of the first column as name for the output file and excludes the first column for contents. However, for the other files a column with the numbers of the first column is also included. For instance for M2524. The name of the created file is working well (M2524), but when I open the file the columns are:
2524 2 4 5
Whereas I want to exclude 2524 as succeeded for M26 and M632. Guess this has to do with the substr and match criteria in the command, but don't really understand how this works and how to solve this....
Yes. You're right, it's not a robust solution since the second field can be a substring of the first one. This is independent from the actual content and prints out the fields from the second to the last:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.