HemaV
September 19, 2011, 8:28am
1
Hi All,
I want my file to be split based on value of 'N' (passed as argument). If value of 'N' is '2' then 4 new files will be generated from the below source file and the o/p file shoud look like File_$num , where num will be incremental.
Source file:
1
2
3
4
5
O/p Files:
File_001.txt
1
2
File_002.txt
3
4
File_003.txt
5
Thanks in Advance,
ln=2
split -l $ln inputfile_name
one way...
HemaV
September 19, 2011, 8:36am
3
how to give file name as File_$num
where num is incremental as 1, 2, 3........
Thanks
sk1418
September 19, 2011, 8:51am
4
kent$ seq 10|awk '{f=(NR%2)==0?NR/2:int(NR/2+1); print > f".txt"}'
kent$ head {1..5}.txt
==> 1.txt <==
1
2
==> 2.txt <==
3
4
==> 3.txt <==
5
6
==> 4.txt <==
7
8
==> 5.txt <==
9
10
Change val as required.
val=2
awk -v val="$val" 'BEGIN{i=1}{if(t==1){i++;t=0}}
{if($0%val==0){t=1};file="File_"i".txt";print >>file}' inputFile
if Solaris, please use nawk
--ahamed
HemaV
September 19, 2011, 9:19am
6
Thanks above code works
---------- Post updated at 08:14 AM ---------- Previous update was at 08:11 AM ----------
When I use val=4 (i expected 4 records in File_3.txt but getting 2 records)
awk -v val="$val" 'BEGIN{i=1}{if(t==1){i++;t=0}}
{if($0%val==0){t=1};file="File_"i".txt";print >>file}' inputFile
o/p:
$ cat File_1.txt
1
2
3
4
$ cat File_2.txt
5
6
7
8
$ cat File_3.txt
9
0
$ cat File_4.txt
11
12
$ cat File_5.txt
13
14
15
---------- Post updated at 08:17 AM ---------- Previous update was at 08:14 AM ----------
sk1418: without "seq 10" code is working fine. Thanks
---------- Post updated at 08:19 AM ---------- Previous update was at 08:17 AM ----------
sk1418: Contencts are getting split based on number but my files are coming with only numbers and not with f1.txt, f2.txt...
O/p file names:
4.txt
3.txt
2.txt
1.txt
Thanks
[root@D152153 tmp]# cat input_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@D152153 tmp]# awk -v val="$val" 'BEGIN{i=1}{if(t==1){i++;t=0}}
{if($0%val==0){t=1};file="File_"i".txt";print >>file}' input_file
[root@D152153 tmp]# cat File_1.txt
1
2
3
4
[root@D152153 tmp]# cat File_2.txt
5
6
7
8
[root@D152153 tmp]# cat File_3.txt
9
10
11
12
[root@D152153 tmp]# cat File_4.txt
13
14
15
Its working for me. Can you paste your input file?
--ahamed