[Solved] Split file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Split file using awk
# 1  
Old 12-16-2011
[Solved] Split file using awk

hlow all,
need your advice
i have sample.txt
Code:
1252468812,yahoo,3.5
1252468812,hotmail,2.4
1252468819,yahoo,1.2
1252468812,msn,8.9
1252468923,gmail,12
1232468819,live,3.4
1252368929,yahoo,9.0
1252468929,msn,1.2

now i want filtering with awk so output will like this

12524_log.txt
Code:
1252468812,hotmail,2.4
1252468819,yahoo,1.2
1252468812,msn,8.9
1252468923,gmail,12
1252468929,msn,1.2

12324_log.txt
Code:
1232468819,live,3.4

12523_log.txt
Code:
1252368929,yahoo,9.0

i using
Code:
awk -F "," '{close(f);f=$1}{print > f".txt"}' sample.txt

but this not like output i want get substr($1,0,5) not all $1
how i can do that

thx for your advice

---------- Post updated at 08:48 PM ---------- Previous update was at 08:03 PM ----------

Quote:
Originally Posted by zvtral
hlow all,
need your advice
i have sample.txt
Code:
1252468812,yahoo,3.5
1252468812,hotmail,2.4
1252468819,yahoo,1.2
1252468812,msn,8.9
1252468923,gmail,12
1232468819,live,3.4
1252368929,yahoo,9.0
1252468929,msn,1.2

now i want filtering with awk so output will like this

12524_log.txt
Code:
1252468812,hotmail,2.4
1252468819,yahoo,1.2
1252468812,msn,8.9
1252468923,gmail,12
1252468929,msn,1.2

12324_log.txt
Code:
1232468819,live,3.4

12523_log.txt
Code:
1252368929,yahoo,9.0

i using
Code:
awk -F "," '{close(f);f=$1}{print > f".txt"}' sample.txt

but this not like output i want get substr($1,0,5) not all $1
how i can do that

thx for your advice
done i solve this case

Last edited by radoulov; 12-17-2011 at 06:20 PM.. Reason: Marked as solved!
# 2  
Old 12-30-2011
convert script awk

dear all

how to convert my script like this
Code:
awk -F "," '{close(f);f=$1}{print > f".txt"}' sample.txt

for using in solaris iam first in solaris so all my script in linux not alot of working in solaris

thx for advice
# 3  
Old 12-30-2011
Code:
nawk '{a=substr($0,1,5)"_log.txt"; printf("%s\n",$0) > a}' sample.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split file using awk

I need to split the incoming source file in to multiple files using awk. Split position is (6,13) : 8 positions All the records that are greater than 20170101 and less than or equal to 20181231 should go in a split file with file name as source... (11 Replies)
Discussion started by: rosebud123
11 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Split one file into more than one file

Guys, I have an input file MGC001|108.28|-2.86489|100-120|MANGGAC MGC002|108.071|-2.69028|80-100|KELAPA KAMPIT MGC003|108.168|-2.97053|50-80|GANTUNG MGC007|108.192722222|-2.766138889|0-50|KELAPA KAMPIT MGC008|108.11075|-3.002666667|0-50|GANTUNG... (10 Replies)
Discussion started by: radius
10 Replies

3. Shell Programming and Scripting

awk file split

Hi all, First of all I' like to mention that I'm pretty new to unix scripting. :( I'm trying to split an large xml with awk and rename it based on the values of two attributes. Example XML <RECORD> <element1>11</element1> <element2>22</element2> <element3>33</element3>... (18 Replies)
Discussion started by: f0usk4s
18 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Perl Question - split function with csv file

Hi all, I have a csv file that appears as follows: ,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,now when i use the split function like this: my @f = split/,/; the split function will split the data that is... (2 Replies)
Discussion started by: WongSifu
2 Replies

5. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

6. Shell Programming and Scripting

[Solved] Script to split a file into two

Hi i have a file like a 12 b 13 c 14 d 15 I want to split it based on a blank line like in first file I should have a 12 b 13 and in the second file I have c 14 d 15 How can i do this? Any help will be greatly appreciated (5 Replies)
Discussion started by: prarat
5 Replies

7. Shell Programming and Scripting

How to split a file using AWK?

Hello, I have a file like the following: david,a,b,c,20,r thomas,a,b,c,30,r willaiam,a,b,c,80,r barbara,a,b,c,100,r I would like to split the file into other files using a condition for the contents of column 5. The condition should be a if the contents of column 5 is in a range... (4 Replies)
Discussion started by: keenboy100
4 Replies

8. Shell Programming and Scripting

split a file using awk

Hi , I just need to split a file and outputfiles are redirected to gzip file need: Input file - A.gz content of A.gz is 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200112|sdfdf 100|sfdds|dffdds|200212|sdfdf 100|sfdds|dffdds|200212|sdfdf... (3 Replies)
Discussion started by: mohan_xunil
3 Replies

9. Shell Programming and Scripting

split file with awk

I did a lot of search on this forum on spiting file; found a lot, but my requirement is a bit different, please guide. Master file: x:start:5 line1:23 line2:12 2:90 x:end:5 x:start:2 45:56 22:90 x:end:2 x:start:3 line1:23 line2:12 x:end:3 x:start:2 line5:23 (1 Reply)
Discussion started by: uwork72
1 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question