Conditional Splitting.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional Splitting.
# 1  
Old 12-08-2009
Conditional Splitting.

hi,

I have file with some data delimited by #.

For e.g. :
Code:
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

Now, I would like to split the file. Normally if I use the split command, it would split the line into files based on the -l parameter. But I would like to split the file from the point where the column 2 changes.

E.g;
Code:
RHMS0001#1#ABCD
RHMS0002#1#ABCD
RHMS0003#1#ABCD
RHMS0004#1#ABCD
RHMS0005#1#ABCD
RHMS0006#1#ABCD
RHMS0007#1#ABCD
    in first file...

Code:
RHMS0001#2#ABCD
RHMS0002#2#ABCD
    in second...

Code:
RHMS0001#3#ABCD
RHMS0004#3#ABCD
RHMS0006#3#ABCD
    in third

Can someone suggest some way to do this.

thanks in advance.

Last edited by zaxxon; 12-08-2009 at 03:33 AM.. Reason: code tags
# 2  
Old 12-08-2009
Code:
$> awk -F"#" '$2 != a {a=$2; print $0 >> "outfile"a} {print $0 >> "outfile"$2}' infile
$> ll out*
-rw-r--r-- 1 root root   128 2009-12-08 08:38 outfile1
-rw-r--r-- 1 root root    48 2009-12-08 08:38 outfile2
-rw-r--r-- 1 root root    64 2009-12-08 08:38 outfile3
$> cat outfile1
RHMS0001#1#ABCD
RHMS0001#1#ABCD
RHMS0002#1#ABCD
RHMS0003#1#ABCD
RHMS0004#1#ABCD
RHMS0005#1#ABCD
RHMS0006#1#ABCD
RHMS0007#1#ABCD
$> cat outfile2
RHMS0001#2#ABCD
RHMS0001#2#ABCD
RHMS0002#2#ABCD
$> cat outfile3
RHMS0001#3#ABCD
RHMS0001#3#ABCD
RHMS0004#3#ABCD
RHMS0006#3#ABCD

# 3  
Old 12-08-2009
Try this:

Code:
awk -F"#" 'c != $2 {c++}{print > "file" c}' file

# 4  
Old 12-08-2009
MySQL

Thanks a ton. Both the codes work perfectly fine. And sorry for for not formatting the message. shall take care. Cheers !!!
# 5  
Old 12-10-2009
How about this?
Code:
awk -F# '{ print >>"out"$2 }' < file

# 6  
Old 12-10-2009
Quote:
Originally Posted by dennis.jacob
How about this?
Code:
awk -F# '{ print >>"out"$2 }' < file

Awesome!!
# 7  
Old 12-10-2009
Quote:
Originally Posted by dennis.jacob
How about this?
Code:
awk -F# '{ print >>"out"$2 }' < file


Superb !!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional OR in shell

Hi I have been trying to write a simple code: if ] || ] then echo "Log Directory is not empty, we will continue with archive operation" else echo "Log Directory is empty, Exiting......." exit 1 fi It never checks for the second OR condition i.e. ] Could you please help? Thanks (8 Replies)
Discussion started by: ankur328
8 Replies

2. Shell Programming and Scripting

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: >Id1 textA >Id2 textBand my outputs file (2 in this case) shoul be: Id1 >Id1 textAId2 >Id2 textBhope you may help me. Best G (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

3. Shell Programming and Scripting

Conditional awk

Hello All, I have a file like this: bash-3.00$ cat 1.txt 201112091147|0|1359331220|1025 201112091147|0|1359331088|1024 201112091144|0|1359331172|1025 201112091147|0|1359331220|1021 201112091149|0|1359331088|1027 201112091144|0|1359331172|1029 and a list of MSISDNs in another file... (9 Replies)
Discussion started by: EAGL€
9 Replies

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

5. Shell Programming and Scripting

Conditional replacements

Hi, I have a requirement as below Input Jacuzzi,"Jet Rings, Pillows",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,, Jacuzzi,"Jet Rings, Pillows, Skirt/Apron",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,, Output Jacuzzi,"Jet Rings!@%... (6 Replies)
Discussion started by: kewk
6 Replies

6. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

7. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

8. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies

9. UNIX for Dummies Questions & Answers

conditional ftp

There are two servers X and Y.We have some files in server X created in the month of may,08(other months file is also there). We want to FTP the files to server Y checking the condition on server X.We cant run script on server X.please suggest wat is the command to do it (3 Replies)
Discussion started by: dr46014
3 Replies

10. Shell Programming and Scripting

conditional split

Hi, Can someone let me know how I can split a record when it contains a vairable length of fields. Line1 field1,field101,field102,field 103,field104 Line 2 field1,field101,field102,field 103,field104,field201,field202,field 203,field204 Line 3 field1,field101,field102,field... (5 Replies)
Discussion started by: braindrain
5 Replies
Login or Register to Ask a Question