0

I have a file which is about 10 gigs and therefore can not be process it in my beloved excel. I need to cut this file in multiple parts based on the first number. For example the file looks something like this.

0.213;89;230;0.1234    
0.213;323;650;0.3500    
0.213;8234;10;0.7863
...
0.653;89;230;0.1234    
0.653;323;650;0.3500    
0.653;8234;10;0.7863
..
1.213;89;230;0.1234
1.213;323;650;0.3500
1.213;8234;10;0.7863

where the dots represent the fact that there are approximatly 200 lines following with 0.213. What I actually want to achieve is that I use awk, csplit or something like that. to split this huge file into multiple smaller files purely based on the first number. so..... 0.213 and 0.653 should be placed in the one file and 1.213 and whatever follows which starts with 1.xxx in the next file.

2

With awk, you'd do:

awk '{file = "output" substr($0, 1, 1) ".txt"; print > file}' < input.txt

Note that lines for 10.123 will go into output1.txt just like the lines for 1.123 or 11.123, 123123.123...

Alternatively, you could do:

awk -F'[.;]' '{file = "output" $1 ".txt"; print > file}' < input.txt

For the files to be named based on the content of the line up to the first . or ; (lines starting with 10.123;... or 10;... going into output10.txt).

  • thank you for your replay. I do indeed want 10.123 and 11.123 to be separated into separate files. Would that last command satisfy that? – Zak.M Apr 24 '18 at 9:19
  • is there any reason you are not using awk '{print >"output" substr($0, 1, 1) ".txt"}' infile? – αғsнιη Apr 24 '18 at 11:20
  • @αғsнιη, iirc some awk implementations (OpenBSD?) struggle to differentiate the ">" redirection operator from the ">" comparison operator in some non-simple contexts. In doubt, I make sure the target of a redirection is a simple variable. – Stéphane Chazelas Apr 24 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.