Conditional splitting in more file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional splitting in more file
# 1  
Old 11-26-2015
Conditional splitting in more file

Dear All,
I was wondering in how split one file in multiple file in a conditional way.

Briefly, I have a file like this:

Code:
>Id1
textA
>Id2
textB

and my outputs file (2 in this case) shoul be:

Id1
Code:
>Id1
textA

Id2
Code:
>Id2
textB

hope you may help me.

Best

G
# 2  
Old 11-26-2015
Hello giuliangiuseppe,

Could you please try following and let me know if this helps.
Code:
awk '/^>/{++i}{print >> "file" i}'  Input_file

Output will be as follows.
Code:
cat file1
>Id1
textA

cat file2
>Id2
textB

Thanks,
R. Singh
# 3  
Old 11-26-2015
It works prefectly.
The only things is that should be nice that file name is the first line of each file (without
Code:
>

)

Code:
cat Id1
>Id1
textA

cat Id2
>id2
textB

Best

Giuliano
# 4  
Old 11-26-2015
Try like
Code:
awk '/^>/{FN=$0; sub (">", "", FN)}{print > FN}'  file
Id1:
>Id1
textA
Id2:
>Id2
textB

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-26-2015
Hello Giuliano,

Could you please try following then, it may help you in same.
Code:
awk '/^>/{sub(/>/,X,$0);i=$0} {print >> i}' Input_file

Output will be as follows.
Code:
cat Id1
Id1
textA
  
cat Id2
Id2
textB

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 11-26-2015
If your input file is to be split into several files, you may have to close files when you're done writing to them. Even if you just have a few file to be written, it is a good idea to close files when you're done with them...

If you run into problems with the other suggestions you've been given, you could try:
Code:
awk '/^>/{close(f);f=substr($0,2)}{print > f}' file

As always, if you want to try any of these awk script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bashrc File - Conditional Command Execution?

Hello All, I was wondering if there is a way to execute a command in my ".bashrc" file based on how I logged into the PC? I was thinking maybe there is a way to check how the user (*myself) logged in, maybe somehow with the who command along with something else, but I'm not sure... I know I... (7 Replies)
Discussion started by: mrm5102
7 Replies

2. Shell Programming and Scripting

Conditional replacement of columns in a text file

Hello scriping expert friends, I have 2 requirements on replacing fields of text files: I have lot of data with contents like below: Requirement-1: The digit after 0 should always be changed to 1 (3 Replies)
Discussion started by: magnus29
3 Replies

3. Shell Programming and Scripting

Conditional File Selection From a Directory

I have a directory with files that was saved all along the day at different times. Every day I have to run a program that will select 50 files for each hour which has the maximum score. Sample file name: a_b_c_d_e_f where a,b,c,d,e,f are double values Score calculation: (a + b + c + d + e... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

4. Programming

Conditional replace after reading in a file

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (7 Replies)
Discussion started by: naveen@
7 Replies

5. Shell Programming and Scripting

Conditional File Splitting in Unix

Hello Guys, I am new to Unix and i got one requirement where I need to split my file into maximum 5 files on below conditions, if they are splitted into less than 5 then also we are fine. Columns D value should appear in a single file only and shouldn't continue in other files. ... (1 Reply)
Discussion started by: Rizzu155
1 Replies

6. Shell Programming and Scripting

conditional append one line in file.

Hi, Unix gurus, I have a requirement as following: checking existing file, if the file only contain one line. then append "No data" else keep existing file as is. can i achieve this by in command line without write a script. :wall: Thanks in advance. (4 Replies)
Discussion started by: ken002
4 Replies

7. Shell Programming and Scripting

modify a file by inserting a conditional values

Hi, input file CCCC 1204 215764.85 9405410.40 1189 DDDD 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540 DDDD 5199 1556 5278 1567 5529 1603 5674 1614 6076 1915 DDDD 6605 2371 7004 2779 CCCC ... (4 Replies)
Discussion started by: Indra2011
4 Replies

8. Shell Programming and Scripting

Perform Operations on One File Conditional on Data in Another File

Hello all, I am looking for a solution to the following problem. Perl or python solutions also welcome. Given this input: And this input: I want to get this output. The rule being that if the number in the first file is < 0.9, then the corresponding two columns on... (2 Replies)
Discussion started by: hydrabane
2 Replies

9. Shell Programming and Scripting

Conditional Splitting.

hi, I have file with some data delimited by #. For e.g. : RHMS0001#1#ABCD RHMS0002#1#ABCD RHMS0003#1#ABCD RHMS0004#1#ABCD RHMS0005#1#ABCD RHMS0006#1#ABCD RHMS0007#1#ABCD RHMS0001#2#ABCD RHMS0002#2#ABCD RHMS0001#3#ABCD RHMS0004#3#ABCD RHMS0006#3#ABCD (7 Replies)
Discussion started by: pparthiv
7 Replies

10. Shell Programming and Scripting

Conditional reverse of certain file line order

Hi I am brand new to programming, I dont know how to go about this task, or what language is best for this...If there is an easy solution in different languages, I would love to see them. I want to learn about the steps to take on this, so Please put in comments where code is used. I believe in... (9 Replies)
Discussion started by: perlrookie
9 Replies
Login or Register to Ask a Question