Split a files into many files when condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a files into many files when condition
# 1  
Old 04-26-2010
Split a files into many files when condition

Hi Everyone,

file.txt
Code:
+++
a
b
c

+++
d


+++
asdf fefe
fff

Would like to have the output:
file1.txt
Code:
+++
a
b
c

file2.txt
Code:
+++
d

file3.txt
Code:
+++
asdf fefe
fff

Any simple way to do that, like awk, perl.

Thanks

Last edited by jimmy_y; 04-26-2010 at 06:51 AM..
# 2  
Old 04-26-2010
Hi,


Do you want to separate the records into file base on '+++' ?
Could you explain more about your issue ?
# 3  
Old 04-26-2010
Quote:
Originally Posted by pravin27
Hi,


Do you want to separate the records into file base on '+++' ?
Could you explain more about your issue ?
Thanks, yes, want to split is when there is '+++'. (just now made a mistake for file3.txt, already corrected the output)
# 4  
Old 04-26-2010
Try this:
Code:
awk /^+++/{c++}{print > "file" c ".txt"}' file.txt

# 5  
Old 04-26-2010
Code:
$ cat buf
+++
a
b
c

+++
d


+++
asdf fefe
fff
$ awk '/^\+/ { f=++i".txt" } { print >> f }' buf
$ ls [1-3].txt
1.txt   2.txt   3.txt
$ cat -n [1-3].txt
     1  +++
     2  a
     3  b
     4  c
     5
     1  +++
     2  d
     3
     4
     1  +++
     2  asdf fefe
     3  fff


Last edited by agn; 04-26-2010 at 07:20 AM.. Reason: don't need close()
# 6  
Old 04-26-2010
Quote:
Originally Posted by Franklin52
Try this:
Code:
awk /^+++/{c++}{print > "file" c ".txt"}' file.txt

Smilie work perfect, if i want to remove those empty lines (only beginning and ending of each file, not the middle empty line) also in each file[1-3].txt, please advice. Thanks
# 7  
Old 04-26-2010
To remove empty lines:

Code:
$ awk '/^\+/ { f=++i".txt" } !/^$/{ print >> f }' buf

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. UNIX for Beginners Questions & Answers

Compare between two files with condition

Hello there. I am trying to compare two files. File1 Austria Mobile 1 United Kingdom Mobile 1 ... File2 Austria Mobile Vien 2 Austria Mobile Ostr 0 United Kingdom Mobile Dev 0.7 United Kingdom Mobile OST 1.5 What i want to do is to compare both files and... (12 Replies)
Discussion started by: dragonfly85
12 Replies

3. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

4. Shell Programming and Scripting

Split files

Hi , I have 100 records in a.txt file Need to split the a.txt file in to 5 files 1ST File: ex: My file name should be a1.txt - line count in file should be 1 to 15 2ND File: ex: My file name should be a2.txt - line count in file should be 16 to 40 3ND File: ex: My file name... (1 Reply)
Discussion started by: satish1222
1 Replies

5. Shell Programming and Scripting

Merge two files by condition

I have two files as below A file /* comment for id1 */ "id1" = "x1" /* comment for id2 */ "id2" = "x2" /* comment for id3 */ "id3" = "x3" B file /* comment for id1 */ "id1" = "y1" /* comment for id2 */ "id2" = "x2" (22 Replies)
Discussion started by: mikezang
22 Replies

6. Shell Programming and Scripting

Merging two files with condition

I have two files of the type 111 222 10 112 223 20 113 224 30 114 225 20 and 111 222 9 444 555 8 113 224 32 666 777 25 I want to merge files based on 1 and 2nd column. if 1st and 2nd column are unique in file 1 and 2 keep... (3 Replies)
Discussion started by: digipak
3 Replies

7. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

8. UNIX for Dummies Questions & Answers

split files into specified number of output files

Hi everyone, I have some large text files that I need to split into a specific number of files of equal size. As far as I know (and I don't really know that much :)) the split command only lets you specify the number of lines or bytes. The files are all of a different size, so the number of... (4 Replies)
Discussion started by: Migrainegirl
4 Replies

9. Shell Programming and Scripting

List files with a certain condition

Guys help me out here.... I have the following var: date_system=20080507 And I have a folder with many files with the following pattern: test_%date%_reference, like test_20080201_tokyo.csv test_20080306_ny.csv and so on... What I need is: 1) list all files and them compare the... (1 Reply)
Discussion started by: Rafael.Buria
1 Replies

10. Shell Programming and Scripting

remove some files on a condition..

Hi.. when I do a ls -lt, I get a listing of about 200 files.. These are trace files and some of it I might not need.. To be clear, say in a given week , I might not need files that have been traced between 11 and 11:30 am on a particular day. How can I delete based on this condition ? Thanks,... (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question