split file content into specific folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split file content into specific folders
# 1  
Old 06-18-2012
split file content into specific folders

Hi
I have a large text file and I want to split its content into multiple flies.
this large file contains several blocks of codes separated by a comment line for each block.
this comment line represents a directory path

So, when separate these blocks each into a separate file, This output file should be in the same name and directory of its block comment.

an example of the large file content is:
Code:
//home/user/app1/lib/note1.txt
xyz
xyz
zylij
//home/user/app1/core/note2.txt
xyz
xyz
zylij

The result will be 2 files:
note1.txt file is created in the path //home/user/app1/lib

and

note2.txt file is created in the path //home/user/app1/core

and so on

Kindly, can you help in writing such a script

Thank you

Last edited by jim mcnamara; 06-19-2012 at 05:13 PM..
# 2  
Old 06-18-2012
Code:
awk -v RS="//" '{ print > "//"$1 }' inputfile

Use nawk on solaris.
# 3  
Old 06-18-2012
Didn't work

awk: cannot open "//" for output (Is a directory)

---------- Post updated at 02:39 AM ---------- Previous update was at 12:33 AM ----------

I manage to make it work , only when I remove the // from the first line of the large file and keep the subsequent //home/xxxx directory path entries

However, when a block does contain another comment (it is also // but not the //home/xxx directory path), it will cut that file and wont create the blocked file fully. i.e it will crate the block file but not complete!

What I need is to create separate files from the large file , while each block is identified by the full path of the directory:
//home/user/app1/lib/note1.txt

rather than just //

I hope I am making myself understandable

thank you
# 4  
Old 06-19-2012
I'm afraid you aren't.

What things did you change to make it work?

What did it then do?
# 5  
Old 06-19-2012
Would this solve the splitting of the content into multiple files?

All files in same directory:
Code:
awk 'BEGIN{FS="/"}{if($NF ~ /note.*txt/) y=$NF; if($NF !~ "txt") print $0 > y }' infile

or

All files in given directory
Code:
awk 'BEGIN{FS="//"}{if($NF ~ /note.*txt/) y=$NF; if($NF !~ "txt") print $0 > y }' infile

# 6  
Old 06-19-2012
Note: Posix awk will only allow RS to consist of a single character.
# 7  
Old 07-01-2012
still not working Smilie

Here is an example of the original file (I will call it x1) that I want to split it into multiple text files (t1.txt, t2.txt and t3.txt) in the directory /home/turki/Downloads/widd (which is already exist)

this is the content of the original file x1

Code:
/home/turki/Downloads/widd/t1.txt
t1 file starts here
/****************************************************************************
WISO PROJECT Version : 1.5 //some comment here
end t1
//home/turki/Downloads/widd/t2.txt
t2 file starts here
/****************************************************************************
WConsole Class
end t2
//home/turki/Downloads/widd/t3.txt
t3 file starts here
/****************************************************************************
WInstance Class
end t3

When I use the command:
Code:
awk -v RS="//" '{ print > "//"$1 }' x1

it does split the content of the x1 file into 3 files t1.txt, t2.txt and t3.txt each in its specified location (inside the "widd" directory)
However, the problem is that if the section of the file contains a comment (like in t1.txt section //some comment here) the split of that file stops there and does not show the whole content of t1 section in the t1.txt file. But I have no problem with t2 and t3 as the content for t2 and t3 does the perfect job of copying the whole content to their appropriate txt files.

How can I split the content of the t1 section EVEN with the comment inside it to the new t1.txt file?

Thank you,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying specific file types to specific folders

I am trying to write a script that cycles through a folder containing many folders and when inside each one it's supposed to copy all the .fna.gz files to a folder elsewhere if the file and the respective folder have the same name. for fldr in /home/playground/genomes/* ; do find .... (8 Replies)
Discussion started by: Mr_Keystrokes
8 Replies

2. Shell Programming and Scripting

Split a content in a file with specific interval base on the delimited values using UNIX command

Hi All, we have a requirement to split a content in a text file every 5 rows and write in a new file . conditions: if 5th line falls between center of the statement . it should look upto after ";" files are below format: 1 UPDATE TABLE TEST1 SET VALUE ='AFDASDFAS' 2 WHERE... (3 Replies)
Discussion started by: KK230689
3 Replies

3. Shell Programming and Scripting

Move multiple files 4rm Source to different target folders based on a series num in the file content

Dear Experts my scenario is as follows... I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims". All files are getting... (17 Replies)
Discussion started by: phani333
17 Replies

4. Shell Programming and Scripting

Search for specific file type in subdirectory with multiple folders

I have a directory that is in the below order (the --- is not part of the directory tree, only there to help illustrate: DATE --- main level Folder1 --- level under DATE plugin_out --- level under Folder1 variantCaller_out.40 --- level under plugin_out 001,002,003 --- level under... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

awk : split file and rename and save in path according to content

Hello, I'm using Windows 7 ; sed, awk and gnuwin32 are installed. I have a big text file I need to manipulate. In short, I will have to split it in thousands of short files, then rename and save in a folder which name is based upon filename. Here is a snippet of my big input.txt file (this... (4 Replies)
Discussion started by: sellig
4 Replies

6. Shell Programming and Scripting

Compare two folders and file content

Hi, I am having two folders where i need to compare the content of files and also to know if any new files been added and redirect the difference output in respective filename logs. For e.g.: Directory D1: f1 f2 f3 Directory D2: f1 f2 f3 f4 i Need to compare the directories... (25 Replies)
Discussion started by: rohit_shinez
25 Replies

7. Shell Programming and Scripting

Split a file in more files based on score content

Dear All, I have the following file tabulated: ID distanceTSS score 8434 571269 10 10122 393912 9 7652 6 10 4863 1451 9 8419 39 2 9363 564 21 9333 7714 22 9638 8334 9 1638 1231 11 10701 918 1000 6587 32056 111 What I would like to do is the following, create 100 new files based... (5 Replies)
Discussion started by: paolo.kunder
5 Replies

8. Shell Programming and Scripting

Split the file based on the content

Arun kumar something somehting Enterting in to the line . . . . Some text text Finshing the sentence Some other text . . . . Again something somehting Enterting in to the line . . . . . . Again text text Finshing the sentence (6 Replies)
Discussion started by: arukuku
6 Replies

9. Shell Programming and Scripting

split file content

Hi All; I have input file like below name char(3) number number(3) inputfile namenumber xyz123abc509kai330 aca203 ald390afa000als303 I wanted to split like below:- output like this:- xyz123 abc509 kai330 aca203 ald390 (6 Replies)
Discussion started by: Jairaj
6 Replies

10. Shell Programming and Scripting

split file depending on content

Hi, I have a file which contains records of data. I need to split the file into multiple files depending upon the value of last field. How do i read the last field of each record in the file??? Regards, Chaitrali (4 Replies)
Discussion started by: Chaitrali
4 Replies
Login or Register to Ask a Question