The UNIX and Linux Forums  

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
How to remove duplicate records with out sort svenkatareddy Shell Programming and Scripting 19 06-11-2008 03:10 PM
How to remove duplicate records with out sort svenkatareddy SUN Solaris 2 02-28-2008 08:38 AM
How to Sort Records Uniquely? earnstaf UNIX for Dummies Questions & Answers 12 02-19-2008 09:23 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 01:15 PM
dynamically split the records braindrain Shell Programming and Scripting 6 11-04-2005 11:52 AM

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

Join Date: Aug 2008
Posts: 17
Sort & Split records in a file

Hi,

I am new to scripting. I need a script to sort and the records in a file and then split them into different files.
For example, the file is:

H1......................
H2......................
D2....................
D2....................

H1........................
H2........................

Etc.,

Here the file should be sorted and splitted in such a way that all H1 records will be put into one file. all H2 in other file and so on.

Pls help.

Regards,
Sunitha
  #2 (permalink)  
Old 08-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Try this?


Code:
sort file | awk '{ file=substr($0,1,2); print >> file }'

  #3 (permalink)  
Old 08-05-2008
Sunitha_edi82 Sunitha_edi82 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 17
Hi,

Thanks for the reply.

I have written the script something like this.

#!/usr/bin/ksh
#############################################################################
#----------------------------------------------------------------------------
#Check for the input files.
#----------------------------------------------------------------------------
cd /app/chdata/workflow/suppl/esoutput/spd/flatfile
sort /app/chdata/workflow/suppl/esoutput/spd/flatfile/testfile | awk '{ file=substr($0,1,2); print >> /app/chdata/workflow/suppl/esoutput/spd/flatfile/testfile1_out.txt }'

However when I tried executing it says cannot execute.

Also, is my script right?

If not pls correct me.

Regards,
Sunitha
  #4 (permalink)  
Old 08-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380

Code:
#!/usr/bin/ksh
cd /app/chdata/workflow/suppl/esoutput/spd/flatfile
sort testfile |
 awk '{ file=substr($0,1,2)".txt"; print >> file }'

Quote:
However when I tried executing it says cannot execute.
Make the file executable with:


Code:
chmod +x /path/to/scriptfile

  #5 (permalink)  
Old 08-05-2008
Sunitha_edi82 Sunitha_edi82 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 17
Hi,

I tried executing the script.

I got the error saying:

syntax error The source line is 1.
The error context is
{ file=substr($0,1,2)".txt"; print >> >>> /app/chdata/workflow/suppl/esoutput/spd/flatfile/testfile1_out. <<< txt }
awk: The statement cannot be correctly parsed.
The source line is 1.
  #6 (permalink)  
Old 08-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
You should leave it as 'file', not replace it with the filename. The variable called 'file' contains the filename it will output to, which is created by taking the first two characters of the input record, and as you have done, adding ".txt" to the end.
  #7 (permalink)  
Old 08-05-2008
Sunitha_edi82 Sunitha_edi82 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 17
Question

Hi,

Now is my script right?

cd /app/chdata/workflow/suppl/esoutput/spd/flatfile
sort testfile1.txt | awk '{file=substr($0,1,2)"testfile1_out.txt";print >> file}'
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:24 AM.


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