Sort & Split records in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort & Split records in a file
# 1  
Old 08-05-2008
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  
Old 08-05-2008
Try this?

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

# 3  
Old 08-05-2008
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  
Old 08-05-2008
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  
Old 08-05-2008
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  
Old 08-05-2008
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  
Old 08-05-2008
Hi,

I tried running it. This time I didnt get any error.

I wrote it as

sort /app/chdata/workflow/suppl/esoutput/spd/flatfile/testfile1.txt | awk '{ file=substr($0,1,2)".txt"; print >> file }'


The script didnt throw any error.

However the output file is 0KB.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Move & Sort by Name - Kick out Bad File Names & More

I have a dilemma, we have users who are copying files to "directory 1." These images have file names which include the year it was taken. I need to put together a script to do the following: Examine the file naming convention, ensuring it's the proper format (e.g. test-1983_filename-123.tif)... (8 Replies)
Discussion started by: Nvizn
8 Replies

2. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

3. Shell Programming and Scripting

Split a large file in n records and skip a particular record

Hello All, I have a large file, more than 50,000 lines, and I want to split it in even 5000 records. Which I can do using sed '1d;$d;' <filename> | awk 'NR%5000==1{x="F"++i;}{print > x}'Now I need to add one more condition that is not to break the file at 5000th record if the 5000th record... (20 Replies)
Discussion started by: ibmtech
20 Replies

4. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

5. Shell Programming and Scripting

How to read records in a file and sort it?

I have a file which has number of pipe delimited records. I am able to read the records....but I want to sort it after reading. i=0 while IFS="|" read -r usrId dataOwn expire email group secProf startDt endDt smhRole RoleCat DataProf SysRole MesgRole SearchProf do print $usrId $dataOwn... (4 Replies)
Discussion started by: harish468
4 Replies

6. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

7. Shell Programming and Scripting

Split a single record to multiple records & add folder name to each line

Hi Gurus, I need to cut single record in the file(asdf) to multile records based on the number of bytes..(44 characters). So every record will have 44 characters. All the records should be in the same file..to each of these lines I need to add the folder(<date>) name. I have a dir. in which... (20 Replies)
Discussion started by: ram2581
20 Replies

8. Shell Programming and Scripting

sort and split file by 2 cols (1 col after the other)

Dear All, I am a newbie to shell scripting so this one is really over my head. I have a text file with five fields as below: 76576.867188 6232.454102 2.008904 55.000000 3 76576.867188 6232.454102 3.607231 55.000000 4 76576.867188 6232.454102 1.555146 65.000000 3 76576.867188 6232.454102... (19 Replies)
Discussion started by: Ghetz
19 Replies

9. Shell Programming and Scripting

sort a file which has 3.7 million records

hi, I'm trying to sort a file which has 3.7 million records an gettign the following error...any help is appreciated... sort: Write error while merging. Thanks (6 Replies)
Discussion started by: greenworld
6 Replies

10. UNIX for Dummies Questions & Answers

How to split multiple records file in n files

Hello, Each record has a lenght of 7 characters I have 2 types of records 010 and 011 There is no character of end of line. For example my file is like that : 010hello 010bonjour011both 011sisters I would like to have 2 files 010.txt (2 records) hello bonjour and ... (1 Reply)
Discussion started by: jeuffeu
1 Replies
Login or Register to Ask a Question