Split large file based on last digit from a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split large file based on last digit from a column
# 8  
Old 05-16-2010
it's the substring in bash
Code:
${<string>:<start>:<length>}

if length is omitted, extracts to end of string like
Code:
${<string>:<start>}

if start is negative, counts from end of string, but it must be in (), like in the given script.
NOTE: first character has index 0.
look there : Manipulating Strings

Last edited by frans; 05-16-2010 at 06:06 AM.. Reason: added link
This User Gave Thanks to frans For This Post:
# 9  
Old 05-16-2010
Code:
awk -F "" '{print > $NF ".txt"}' urfile

# 10  
Old 05-17-2010
Timing comparison

Hi.

Once I process files above 1M lines, I tend to think in terms of {awk,perl}.

My awk code is about the same as that from rdcwayx -- not as clever as that, however: I used length and index functions.

I whipped up a small timing test framework, and created a 600K line file for comparison and easy arithmetic. These are the results:
Code:
% ./run

 Time for awk script on 600000 lines in file data1:

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
printf - is a shell builtin [bash]
specimen (local) 1.17
GNU Awk 3.1.5


-----
 || start [ first:middle:last ]
Edges: 5:0:5 of 600000 lines in file "data1"
298138
201666
307112
739965
187303
   ---
347236
113888
869875
958456
384340
 || end

-----
 Results:
 Processed 600000 lines.

-----
 Size of files:
  60250   60250  421750 f0
  59790   59790  418530 f1
  60266   60266  421862 f2
  59764   59764  418348 f3
  60043   60043  420301 f4
  59937   59937  419559 f5
  60029   60029  420203 f6
  59716   59716  418012 f7
  60110   60110  420770 f8
  60095   60095  420665 f9
 600000  600000 4200000 total

real	0m2.616s
user	0m1.820s
sys	0m0.312s

 Timing for shell loop on 600000 lines in file data1:

real	0m28.599s
user	0m15.465s
sys	0m9.269s

 Total number of lines in 10 files: 600000

So for a 60M file, one could estimate the time needed for a production run (on my workstation) by multiplying the times by 100.

The specific awk code was:
Code:
awk '
	{ digit = substr($0,length($0),1) 
	  print $0 > "f"digit
	}
END	{ print " Processed", NR, "lines." }
' $FILE

and the shell loop was:
Code:
while read L
do
    echo $L >> f${L:(-1)}
done < $FILE

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To Split the file based on column value

Hi Team, I have a requirement in such a way that need to split the file into two based on which column particular value appears.Please find my sample file below. Lets consider the delimiter of this file as either comma or two colons.(:: and ,). So I need to split the file in such a way that all... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. Shell Programming and Scripting

Split file based on a column/field value

Hi All, I have a requirement to split file into 2 sets of file. Below is a sample data of the file AU;PTN;24EX;25-AUG-14;AU;123;SE;123;Test NN;;;;ASD; AU;PTN;24EX;25-AUG-14;AU;456;SE;456;Test NN;;;;ASD; AU;PTN;24EX;25-AUG-14;AU;147;SE;147;Test NN;;;;ASD;... (6 Replies)
Discussion started by: galaxy_rocky
6 Replies

3. UNIX for Dummies Questions & Answers

Split file based on column

i have file1.txt asdas|csada|130310|0423|A1|canberra sdasd|sfdsf|130426|2328|A1|sydney Expected output : on eaceh third and fourth colum, split into each two characters asdas|csada|13|03|10|04|23|A1|canberra sdasd|sfdsf|13|04|26|23|28|A1|sydney (10 Replies)
Discussion started by: radius
10 Replies

4. Shell Programming and Scripting

Help needed - Split large file into smaller files based on pattern match

Help needed urgently please. I have a large file - a few hundred thousand lines. Sample CP START ACCOUNT 1234556 name 1 CP END ACCOUNT CP START ACCOUNT 2224444 name 1 CP END ACCOUNT CP START ACCOUNT 333344444 name 1 CP END ACCOUNT I need to split this file each time "CP START... (7 Replies)
Discussion started by: frustrated1
7 Replies

5. Shell Programming and Scripting

Split the file based on column

Hi, I have a file sample_1.txt (300k rows) which has data like below: * Also each record is around 64k bytes 11|1|abc|102553|125589|64k bytes of data 10|2|def|123452|123356|...... 13|2|geh|144351|121123|... 25|4|fgh|165250|118890|.. 14|1|abc|186149|116657|......... (6 Replies)
Discussion started by: sol_nov
6 Replies

6. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

7. Shell Programming and Scripting

How to split a fixed width text file into several ones based on a column value?

Hi, I have a fixed width text file without any header row. One of the columns contains a date in YYYYMMDD format. If the original file contains 3 dates, I want my shell script to split the file into 3 small files with data for each date. I am a newbie and need help doing this. (14 Replies)
Discussion started by: bhanja_trinanja
14 Replies

8. Shell Programming and Scripting

split the file based on the 2nd column passing as a parameter

I am unable to spit the file based on the 2nd column passing as a parameter with awk command. Source file: “100”,”customer information”,”10000” “200”,”customer information”,”50000” “300”,”product information”,”40000” script: the command is not allowing to pass the parameters with the awk... (7 Replies)
Discussion started by: number10
7 Replies

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

10. Shell Programming and Scripting

split large file based on field criteria

I have a file containing date/time sorted data of the form ... 2009/06/10,20:59:59.950,XAG/USD,Q,1,1115, 14.3025,100,1,1 2009/06/10,20:59:59.950,XAG/USD,Q,1,1116, 14.3026,125,1,1 2009/06/10,20:59:59.950,XAG/USD,R,0,0, , 0,0,0 2009/06/10,20:59:59.950,XAG/USD,R,1,0, 14.1910,100,1,1... (6 Replies)
Discussion started by: asriva
6 Replies
Login or Register to Ask a Question