Sort & Split records in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort & Split records in a file
# 8  
Old 08-05-2008
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}'
# 9  
Old 08-05-2008
This is what I get:

Code:
$ cat sunitha.dat
H1......................
H2......................
D2....................
D2....................
H1........................
H2........................
$ sort sunitha.dat | awk '{ file=substr($0,1,2)".txt"; print >> file }'
$ ls -l
total 64
-rw-r--r--   1 user   users           46 Aug  5 15:50 D2.txt
-rw-r--r--   1 user   users           52 Aug  5 15:50 H1.txt
-rw-r--r--   1 user   users           52 Aug  5 15:50 H2.txt
-rw-r--r--   1 user   users          150 Aug  5 15:44 sunitha.dat
$ cat D2.txt
D2....................
D2....................
$ cat H1.txt
H1......................
H1........................
$ cat H2.txt
H2......................
H2........................
$

# 10  
Old 08-05-2008
Hi,

I tried the same code which you have sent me.

I have the input file in folder
/app/chdata/workflow/suppl/esoutput/spd/flatfile/

and the output files should also be generated in this folder.

My script is in folder /app/com/bin and the script is:

#!/usr/bin/ksh
# This script sorts the input and splits the records into different file


#############################################################################

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


When I run the script, it gets executed and no output files are generated.

Can you help me in this?
# 11  
Old 08-05-2008
What is the output if you change it like this?

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

This won't create any files, it's just for debugging purposes...
# 12  
Old 08-05-2008
Ok but how if I need to create the files seperately where I put the records with all H1's in one file and other file with all H2's and so on?
# 13  
Old 08-05-2008
As I have shown you, my method should work... if you paste the output of the previous command it should help us debug the problem.
# 14  
Old 08-05-2008
Hi,

Can I get help for something like this.

The records in my file should be sorted and the sorted output be redirected to other file.
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