LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-04-2011, 04:29 AM   #1
Mehidi
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Rep: Reputation: Disabled
How to save each line from textfile as new file?


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

Thanks!
 
Old 07-04-2011, 04:33 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
cat file | while read LINE; do echo $LINE > $LINE.txt; done

the filename is pretty useless but that will achieve what you wanted.
 
1 members found this post helpful.
Old 07-04-2011, 05:09 AM   #3
Mehidi
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by acid_kewpie View Post
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!
 
Old 07-04-2011, 05:21 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Mehidi View Post
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:
Code:
awk '{print substr($0,match($0,$2)) >> ( $1 ".txt" )}' file
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:
Code:
awk '{print substr($0,match($0,$2)) >> ( "something_" sprintf("%03d",++c) ".txt" )}' file
Hope this helps.
 
1 members found this post helpful.
Old 07-04-2011, 06:14 AM   #5
Mehidi
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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:
Code:
awk '{print substr($0,match($0,$2)) >> ( $1 ".txt" )}' file
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:
Code:
awk '{print substr($0,match($0,$2)) >> ( "something_" sprintf("%03d",++c) ".txt" )}' file
Hope this helps.
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!
 
Old 07-04-2011, 06:21 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Mehidi View Post
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.
 
Old 07-04-2011, 09:14 AM   #7
Mehidi
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
If I run

awk '{print substr($0,match($0,$2)) >> ( $1 ".txt" )}' filename

for the following data in filename:

M1738 1 3 4
M2524 2 4 5
M8392 3 5 6
M26 4 6 7
M632 5 7 8

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

Thanks guys
 
Old 07-04-2011, 12:58 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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:
Code:
awk '{for (i=2; i<=NF; i++) printf "%s ", $i >> ( $1 ".txt" ); printf "\n" >> ( $1 ".txt" )}' file
 
1 members found this post helpful.
Old 07-04-2011, 08:44 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Another alternative:
Code:
awk '{name = $1".txt";sub(/^[^ ]* /,"");print >> name}' file
 
  


Reply



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
LINUX textfile to WINDOWS textfile bhEsquivel Linux - Software 8 06-03-2011 05:51 PM
Command for appending a text at the start of every line and save it another file ssgupta Linux - Newbie 5 03-10-2011 08:35 AM
[SOLVED] Transferring from save file to save partition hda7 Puppy 2 05-04-2010 08:11 AM
[SOLVED] how to find a word in a textfile starting from the BOTTOM of the textfile ? markraem Linux - Software 3 02-08-2010 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:20 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
Open Source Consulting | Domain Registration