The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help Needed : Split one big file to multiple files monicasgupta Shell Programming and Scripting 5 03-03-2008 07:09 PM
split file depending on content Chaitrali Shell Programming and Scripting 4 11-14-2007 09:15 AM
Split A File Into 2 Files dummy_needhelp Shell Programming and Scripting 7 11-04-2007 07:36 PM
How can I rename multiple files depending on a string occuring in the filenames? karman UNIX for Dummies Questions & Answers 6 05-22-2007 02:29 PM
Split a file into 2 or more files bobo UNIX for Dummies Questions & Answers 4 01-16-2006 05:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-09-2008
deepakgang deepakgang is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Split file into multiple files depending upon first 4 digits

Hi All,

I have a file like below:

1016D"ddd","343","1299"
1016D"ddd","3564","1299"
1016D"ddd","3297","1393"
1016D"ddd","32989","1527"
1016D"ddd","346498","1652"
2312D"ddd","3269","1652"
2312D"ddd","328","1652"
2312D"ddd","2224","2100"
3444D"ddd","252","2100"
3444D"ddd","2619","2100"


I need to split this big file (will contain lot of records like above) into multiple files depending upon the first 4 digits. The output files will be like:

filename.1016.dat

1016D"ddd","343","1299"
1016D"ddd","3564","1299"
1016D"ddd","3297","1393"
1016D"ddd","32989","1527"
1016D"ddd","346498","1652"

filename.2312.dat

2312D"ddd","3269","1652"
2312D"ddd","328","1652"
2312D"ddd","2224","2100"

filename.3444.dat

3444D"ddd","252","2100"
3444D"ddd","2619","2100"


I searched for some examples but they all are done using awk.. and as there is no field separator here I cant use awk..

Please help.

Thanks
D
  #2 (permalink)  
Old 04-09-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
Code:
awk '!/^$/{
 a=substr($0,1,4) 
 print $0 > a".txt"
}' file
  #3 (permalink)  
Old 04-09-2008
deepakgang deepakgang is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Hi,

many thanks.. it worked.
Please could you tell me what the first line of the code does

awk '!/^$/{

Cant I use the code without the !/^$/ part ?


D
  #4 (permalink)  
Old 04-09-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,419
Quote:
Originally Posted by deepakgang View Post
Please could you tell me what the first line of the code does

awk '!/^$/{

Cant I use the code without the !/^$/ part ?
awk '!/^$/ will ignore empty lines.

ghostdog74 Uwin

Last edited by danmero; 04-09-2008 at 01:10 AM..
  #5 (permalink)  
Old 04-09-2008
deepakgang deepakgang is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Thanks Mate..

And excuse me if it sounded like a dumb quest
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0