Multiple File creation from Single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple File creation from Single file
# 1  
Old 04-08-2014
Multiple File creation from Single file

I've one large file with below content
Code:
TD:    0001                   xxxx
thnb
..........
TD:    0001                   yyyy
abcd
.........
.......

TD:    0002                   xxyy
efgh.
...................
...................
TD:    0003                   xxxx
xyvx
................

I want to split the files based on TD: 000n content
So the 1st file will be
Code:
TD:    0001                   xxxx
thnb
..........
TD:    0001                   yyyy
abcd
.........
.......

2nd file will be

Code:
TD:    0002                   xxyy
efgh.
...................
...................

Any idea how can I achieve this?

Thanks!

Last edited by bartus11; 04-08-2014 at 03:23 AM.. Reason: Please use [code][/code] tags.
# 2  
Old 04-08-2014
Try:
Code:
perl -n0e 'while (/TD.*?(?=TD|$)/sg){$x=$&;$x=~/TD:\s*(\w+)/;open $f,">>","$1";print $f $x; close $f}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-08-2014
Or maybe something like:
Code:
awk '/^TD/{close(f);f=$2}{print >> f}' file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 04-08-2014
Hello Franklin52 & Bartus11,
Thanks a lot , your solution has worked perfectly.
Now I have one small problem with another file of similar patern, in my large file in place of "TD:" if I have "RC No./Name :" , ie
Code:
RC No./Name :       0001
........
..............
RC No./Name :       0001
........

RC No./Name :       0002
..........
RC No./Name :       0003

Then above solution is not working if I replace TD with RC.
Any idea why this is not working?

Last edited by Scrutinizer; 04-08-2014 at 07:18 AM.. Reason: code tags
# 5  
Old 04-08-2014
WHAT is not working? I guess it is writing everything to the same file named "No./Name"? Try f=$4 in Franklin52's proposal.
# 6  
Old 04-08-2014
Hello Rudic,
I'm getting below error after following the suggestion
Code:
awk '/^RC No/{close(f);f=$4}{print >> f}' test.txt
awk: (FILENAME=test.txt FNR=1) fatal: expression for `>>' redirection has null string value

 awk '/^RC/{close(f);f=$4}{print >> f}' test.txt
awk: (FILENAME=test.txt FNR=1) fatal: expression for `>>' redirection has null string value


Last edited by Scott; 04-08-2014 at 11:04 AM.. Reason: Use code tags, please...
# 7  
Old 04-08-2014
Quote:
Originally Posted by Steven77
Hello Franklin52 & Bartus11,
Thanks a lot , your solution has worked perfectly.
Now I have one small problem with another file of similar patern, in my large file in place of "TD:" if I have "RC No./Name :" , ie
Code:
RC No./Name :       0001
........
..............
RC No./Name :       0001
........

RC No./Name :       0002
..........
RC No./Name :       0003

Then above solution is not working if I replace TD with RC.
Any idea why this is not working?
In this case you could try:
Code:
awk '/^RC No/{close(f);f=$NF}{print >> f}' test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Script to access multiple file and operate on single file

We have three files as mentioned below: 1. main_file.txt: This is the file in which all operations will be done. Which means this file will be signed by using the below two files 2. signature_file.txt: This is a status file and contains two signatures. 3. command.txt:file contains two commands... (2 Replies)
Discussion started by: chetanojha
2 Replies

3. Shell Programming and Scripting

Multiple file and single parameter file

Hi Team, In our project we have written below 2 scripts like Script1: Shell script start & END Begin Audit process - uses teradata bteq END Audit Process Script 2: Environemtal variable file different Now Client ask to change this requirement and need below files: Script1:... (1 Reply)
Discussion started by: tusharzaware1
1 Replies

4. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

5. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

6. Shell Programming and Scripting

How to make multiple small file out of a single file?

Hi, I have a file that consist of around six million line, now the task is to divide this file into 12 small file so that each file would have half a million lines in it. Thanks. (3 Replies)
Discussion started by: mukulverma2408
3 Replies

7. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. UNIX for Dummies Questions & Answers

how to append into single file for multiple iterations

hi, i want to capture the CPU performance, memory , harddisk usage into a single file for every 10 seconds iterations and it shall run until i kill it. can somebody plz help me in this... thanx (1 Reply)
Discussion started by: bskumar4u
1 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. Shell Programming and Scripting

Single to multiple line file

I am working with single line file with 589744523 characters having 542 "^M" (line feed) character. I want to make 542 different lines file from the single line file thr. shell program only (it can be done thr vi command) rd anil sorry for duplicate post previously, actually i don,t know... (6 Replies)
Discussion started by: anil_kut
6 Replies
Login or Register to Ask a Question