Hi Script Masters I have a strange requirement. Please help.
I am using C shell.
I have a file like the below in sorted order
22
23
25
34
37
45
67
342
456
476
543
677
789
Now I have to split the file in such a way that first 5 of 2 digit number should be saved as aaa.in and the next 5 of 2 digit number should be saved as aab.in and this should continue for all remaning 2 digits (any count) and the first 5 of 3 digit number should be saved in aac.in and the next 5 in aad.in and so on...
In nut shell
file aaa.in should contain
22
23
25
34
37
and file aab.in should contain
45
67
and file aac.in
342
456
476
543
677
and file aad.in should be
789
The split up of 5 number per file is constant and other things will vary..
If you have any doubts please ask me. Any help or clue will be greatly appreciated.
I know split command can create multiple files like aaa,aab but the problem I see it that the filename is not getting carried on .If I use split 2 times it will overwrite aaa file instead of creating aac ..
thanks for the reply rad.
When I tried the code i am getting as
Awk :Syntax error near line 2
Awk : bailing out near line 2.
Can you please tell me what could be wrong.
Actually There might be a file containing around 500,000 lines of numbers so when I split it it might even go for aaa..aab..aac and so on to aba abb abc and so on to aca acb acc and to maximum zzz. From your code I beieve it ca maximum go for aaa to aaz.
The file naming convention is cinaaa.in cinaab.in ...cinbaa...cinzzz.in
What kind of change I have to do in your code for accomadating the above stuffs.
#!/usr/bin/ksh
i=1
j=0
k=0
cat samp | while read line
do
if [ $line -lt 100 ]
then
arr[$i]=$line
if [ $j -le 5 ]
then
echo ${arr[$i]}
echo ${arr[$i]} >> aa$k.txt
i=`expr $i + 1`
j=`expr $j + 1`
fi
if [ $j -eq 5 ]
then
j=0
k=`expr $k + 1`
fi
fi
done
The above script wil split the files for two-digit numbers..
And you complete the rest..
thanks for the reply rad.
When I tried the code i am getting as
Awk :Syntax error near line 2
Awk : bailing out near line 2.
Can you please tell me what could be wrong.
[...]
You should use nawk as suggested in my first reply.
Quote:
Actually There might be a file containing around 500,000 lines of numbers so when I split it it might even go for aaa..aab..aac and so on to aba abb abc and so on to aca acb acc and to maximum zzz. From your code I beieve it ca maximum go for aaa to aaz.
The file naming convention is cinaaa.in cinaab.in ...cinbaa...cinzzz.in
What kind of change I have to do in your code for accomadating the above stuffs.
Hi radoulov
I agree you are a great script master and specialist in nawk. Thanks for the code and it works fine . If you have time, please explain the code. Thanks for the effort and time you took .
Aajan
Thanks for your effort too, actually my requirement is to create file names with cinaaa,aab,aac and so on..but your code creates output as aa0,aa1,aa2 and so on but still I extend my thanks for a different idea on your approach also your code might be little slower for huge data.
Thanks for all who took time to view my problem and took effort to solve it
Hi radoulov
[...]
If you have time, please explain the code.
[...]
Prepare the f array (the alphabet) and set c,c1 and c2 to 1 and n to the number of elements in the f array.
I think that an example will illustrate best this expression:
In other words, we change the filename every time the expression x[length]++ % 5 is 0.
Now, the filename generation.
We need to close the previous file because some Awk implementations (such as Awk on Solaris) can open a limited number of files at the same time.
Then we compose the filename: the f array with rotating keys (from 1 to n, where n is the number of elements in f the array).
hi,
Am able to split an XML file by using follwing awk command,
awk 'NR==1{x=$0;next}/<\/Order>/{print y RS $0 RS "</Order>">f}/Order BillToKey/{f="file"++n".xml";y=x}{y=y RS $0}' filename.xml
but i need to insert a following tag in the begining of every file how to do so. The tag is as... (7 Replies)
Hello,
I want to split or cut a large size log file by year wise(eg 2009, 2010) .But the source file must not have the splited or cut lines after this process ,all of them must move to the destination folder.Does grep command have the fuctionality like cut and paste? I used grep -Ev command but... (17 Replies)
Dear all,
I have thousands of log files in my log directory which I need to zip them and archive. I tried using zip command. But it is not allowing me to archive it more that 4GB of file size. So how to archive them. If it is not possible how to zip all files in to multiple archive files which... (3 Replies)
hi
I have a log file which contains some reports. The log file looks like this:-
STARTOFREPORT
/tmp
file1.txt
some
text
to
be
folowd
ENDOFREPORT
some non utilized
characters
STARTOFREPORT
/log
file2.txt
more
text (3 Replies)
Dear All,
I have a very large file which which i would like split into indvidual frames evrytime the line ends with "ENDMDL" and then name frame1.pdb frame2.pdb etc
can any one give me a few sugeestions? ideally i would like to have ENDMDL at the end of each frame or not pressent at all.
an... (4 Replies)
I have log file whose size is 4 GB , i would like to split it to 1 gb each ,Can any one tell me the syntax of csplit comand for that.
I am using Sun0S 5.8 (3 Replies)
Hi,
I am reading two values from oracle to unix variable and spliting them using the read command as follows,
get_details=`sqlplus -s $sld_user/$sld_password@$sld_string<<EOF
whenever sqlerror exit 1
whenever oserror exit 1
set feedback off
set heading off
set pagesize... (0 Replies)