Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a file...

Status
Not open for further replies.

bondtrails

Technical User
Nov 19, 2002
31
US
Hi all,
I am a bit stuck here with a seemingly simple process: How do I split a file of say X records into n files each file having X/n records?

So if I have a 30,000 record file, how can I split the file into 3 files with each file containing 10,000 records?

Thanks for helping out the newbie everyone!

--Bondster!!
 
man split

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Well if you're on a Unix/Linux platform, then use the 'split' command.

split -l=10000 file
 

Still about split.
This looks difference with the another split command which is usually used in awk, right ?
as far as i know, with that split we can split a file according a separator and put them into an array.
but somehow, i tried them and does not work well.
I don't know if the split can also be used to split a variable or string.. but i guess that should work..

you see,
i do this : split($0,a,&quot;;&quot;)
first, i think that the $0 came from the argument when running the script but still not work.
not work -> when try to have the value of a[0] the value is zero, when supposed to be not,...
any advice would be appreciated...
 
> i do this : split($0,a,&quot;;&quot;)
The first field will be in a[1], not a[0]

So you would do this to print every field
Code:
n = split($0,a,&quot;;&quot;)
for ( i = 1 ; i <= n ; i++ ) print a[i]
 

Hi Salem,
actually, the array are not from index 1.

the whole script like this.

for(i=9; i<=2231; i+=202) {
split($0, a, &quot;;&quot;);
if (a[1] == &quot;P1&quot;) {
x1=a[i+2];
y1=a[i+6]
}
else if (a[1] == &quot;P2&quot;) {
x2=a;
y2=a[i+2]
}
printf(&quot;,%d,%d,%d,%d&quot;,x1,y1,x2,y2)
}' Paysf.txt

still the zeror appear..

Please help
 


to be simple,
here is a test script that i've made :

nawk 'BEGIN { printf(&quot;1,2,3,4,5&quot;);
split($0,a,&quot;;&quot;);
for(i=9; i<=2231; i+=202) {
printf(&quot;%d&quot;,a);
}}' Paysf.txt

result :
1,2,3,4,500000000

Please help me with this.
 
$0 is not defined in the BEGIN section of the AWK script, unless you do an explicit getline from the file.

$0 is set automatically each time around the main pattern/action loop of the AWK program.

Try
Code:
nawk 'BEGIN {
  # this is done once, before the file is opened
  printf(&quot;1,2,3,4,5&quot;);
}
{
  # this is done for every line in the file
  split($0,a,&quot;;&quot;);
  for(i=9; i<=2231; i+=202) {
   printf(&quot;%d&quot;,a[i]);
  }
}' Paysf.txt

PS
Please remember the [ code ][ /code ] tags when posting code. You'll note that the board has taken your
Code:
[i]
and made it italics.
 

Hi Salem,
Thanks for your reply,
Yes, finally, after adding the end-brackets before do the splitting... it works.

But, I have another problem,
Why does the result always repeat ?
If the input file have 7 line,
then, the result will have 7 times which all the same.

Example :
abc1;1;2;3;4;5.... [too long so i have to cut some]
abc2;2;3;4;8;9....
abc1;2;3;4;8;9....
abc2;2;3;4;8;9....
..

Result :
1,2,3,4,5
9
211
413
615
817
1019
1221
1423
1625
1827
2029
2231
9
211
413
615
817
1019
1221
1423
1625
1827
2029
2231
9
... [and so on]

Do I miss something here ?
Please advice...

Thanks a lot.
 
Looks to me like you're printing
Code:
i
rather than
Code:
a[i]
 


Nope, I'm not printing the i,
Somehow I already found out what's wrong..
So, now I'm fixing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top