Split a single file into several others basing on the last column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split a single file into several others basing on the last column
# 1  
Old 01-13-2010
Split a single file into several others basing on the last column

Hi folks,
Happy new year.
I have a file 'filename' that i wd like to split basing on the contents in the last column.
The 'filename' content looks like

256772744788,9,11
256772744805,9,11
256772744792,9,11
256775543055,10,12
256782625357,9,12
256772368953,10,13
256772627735,10,13
256782853530,9,14
256782676830,9,28
256392752548,9,37
256782610403,9,38
256775590939,9,38

and i would like output files like 'file11', 'file12', 'file13', 'file 14', 'file28' etc with content

[cat file11]

256772744788,9,11
256772744805,9,11
256772744792,9,11

[cat file38]
256782610403,9,38
256775590939,9,38

I used this for loop but only created empty files.
[for (( i = 11; i <= 115; i++ )); do grep ',$i' cugnums.csv > file$i;done]

The last column has integer values from 11 to 115
# 2  
Old 01-13-2010
Something like this?

Code:
awk -F, '{print > "file" $3}' OFS="," file

# 3  
Old 01-13-2010
Solution using awk:

Code:
$cat split.awk
#! /usr/bin/awk

{
str="test -f file"$3;
print str
rval=system(str);
if (rval == 1){
        str="echo "$0 "> file"$3
        system(str);
}
else {
        str="echo "$0 ">> file"$3
        system(str);
}
}

Execution:
Code:
$awk -F ',' -f split.awk inputfilename

This script creates files (file11, file12 etc.) in the current directory.
# 4  
Old 01-13-2010
Quote:
Originally Posted by Franklin52
Something like this?

Code:
awk -F, '{print > "file" $3}' OFS="," file

use NF will be better.

awk -F, '{print > "file" $NF}' OFS="," cugnums.csv

---------- Post updated at 07:25 PM ---------- Previous update was at 07:23 PM ----------

Quote:
Originally Posted by jerkesler
I used this for loop but only created empty files.
[for (( i = 11; i <= 115; i++ )); do grep ',$i' cugnums.csv > file$i;done]

The last column has integer values from 11 to 115
Franklin52's script is best solution.

But if you still want to use your own code, fix as below:

Code:
for (( i = 11; i <= 115; i++ )); do grep ",$i$" cugnums.csv > file$i;done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a single file into multiple files based on a value.

Hi All, I have the sales_data.csv file in the directory as below. SDDCCR; SOM ; MD6546474777 ;05-JAN-16 ABC ; KIRAN ; CB789 ;04-JAN-16 ABC ; RAMANA; KS566767477747 ;06-JAN-16 ABC ; KAMESH; A33535335 ;04-JAN-16 SDDCCR; DINESH; GD6674474747 ;08-JAN-16... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

2. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

3. UNIX for Dummies Questions & Answers

Split files into smaller ones with 1000 hierarchies in a single file.

input file: AD,00,--,---,---,---,---,---,---,--,--,--- AM,000,---,---,---,---,---,--- AR, ,---,--,---,--- AA,---,---,---,--- AT,--- AU,---,---,--- AS,---,--- AP,---,---,--- AI,--- AD,00,---,---,---, ,---,---,---,---,---,--- AM,000,---,---,--- AR,... (6 Replies)
Discussion started by: kcdg859
6 Replies

4. Shell Programming and Scripting

Split single file into multiple files using pattern matching

I have one single shown below and I need to break each ST|850 & SE to separate file using unix script. Below example should create 3 files. We can use ST & SE to filter as these field names will remain same. Please advice with the unix code. ST|850 BEG|PO|1234 LIN|1|23 SE|4 ST|850... (3 Replies)
Discussion started by: prasadm
3 Replies

5. UNIX for Dummies Questions & Answers

Split single file into n number of files

Hi, I am new to unix. we have a requirement here to split a single file into multiples files based on the number of people available for processing. So i tried my hand at writing some code as below. #!/bin/bash var1=`wc -l $filename` var2=$var1/$splitno split -l $var2 $1 Please help me... (6 Replies)
Discussion started by: quirkguy
6 Replies

6. Shell Programming and Scripting

Split the single file lines into multiple files

Let's assume that I have a file name called ‘A' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow. INPUT: Input file name A 1 2 3 4 5 6 7 8 9 ........100 Output: 4 output files (x,y,z,w) File x should contains (Skip 4 lines)... (15 Replies)
Discussion started by: subbarao25
15 Replies

7. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies

8. Shell Programming and Scripting

Split a binary file into 2 basing on 2 delemiter string

Hi all, I have a binary file (orig.dat) and two special delimiter strings 'AAA' and 'BBB'. My binary file's content is as follow: <Data1.1>AAA<Data1.2>BBB <Data2.1>AAA<Data2.2>BBB ... <DataN.1>AAA<DataN.2>BBB DataX.Y might have any length, and contains any kind of special/printable... (1 Reply)
Discussion started by: Averell
1 Replies

9. UNIX for Dummies Questions & Answers

split a single sql file into multiple files

Hi,I have a single sql file containing many create table ddl's.Example: CREATE TABLE sec_afs ( rpt_per_typ_c char(1) NOT NULL, rpt_per_typ_t varchar(20) NULL, LOCK ALLPAGES go EXEC sp_primarykey 'sec_afs', rpt_per_typ_c go GRANT SELECT ON sec_afs TO developer_read_only... (5 Replies)
Discussion started by: smarter_aries
5 Replies

10. Shell Programming and Scripting

split single file into many

Hello, We have a text file with more than 1500 paragraphs. There is a blank line to separate paragraphs. We need to create one text file (with any name but unique) per paragraph. In other words, how can i extract each paragraph and create a separate file with those contents (so 1500 text... (3 Replies)
Discussion started by: prvnrk
3 Replies
Login or Register to Ask a Question