Ignore Header and Footer and Sort the data in fixed width file


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-14-2011
Ignore Header and Footer and Sort the data in fixed width file

Hi Experts,

I want to Sort the data in fixed width file where i have Header and Footer also in file.

I m using below commad to do the sort based on field satarting from 15 position to 17 position , but it is not ignoring the Header and Footer of the file while sorting. In the output i am getting both header and footer coming in first and then the data. It is sorting header and footer records also.

Code:
egrep -iv 'head|trail' input_file | sort -t'|' -k1.15,1.17 > output_file

Quote:
Sample input data:
HDD1000000000000220110713AMP 8.3
A100020110712ZZT1K9000A
A100020110712ZZT1J9000L
A100020110712ZZI1G9000A
A100020110712ZZT1M9000L
A100020110712ZZ O9000A
A100020110712ZZ O9000A
A100020110712ZZI1O9000A
A100020110712ZZI1W9000L
TDD1000000000000220110713010113000008

Quote:
Output Data Should be:
HDD1000000000000220110713AMP 8.3
A100020110712ZZ O9000A
A100020110712ZZ O9000A
A100020110712ZZI1G9000A
A100020110712ZZI1O9000A
A100020110712ZZI1O9000A
A100020110712ZZI1W9000L
A100020110712ZZT1M9000L
A100020110712ZZT1K9000A
A100020110712ZZT1J9000L
TDD1000000000000220110713010113000008


Please suggest me on how to sort the data by ignoring header and footer in a file.

Thanks in Advance !
Regards,
-Sasi
# 2  
Old 07-14-2011
Not pretty but...
Code:
H=$(head -1 infile)
$> T=$(tail -1 infile)
$> echo $H; sed '1d;$d' infile| sort -t'|' -k1.15,1.17; echo $T
HDD1000000000000220110713AMP 8.3
A100020110712ZZI1G9000A
A100020110712ZZI1O9000A
A100020110712ZZI1W9000L
A100020110712ZZ O9000A
A100020110712ZZ O9000A
A100020110712ZZT1J9000L
A100020110712ZZT1K9000A
A100020110712ZZT1M9000L
TDD1000000000000220110713010113000008

# 3  
Old 07-14-2011
Since my edit function is not working - here a short addition: The delimeter with sort ("|") is not used, you can leave it out in your example.
# 4  
Old 07-14-2011
Ignore Header and Footer and Sort the data in fixed width file

Thanks for the Reply.

When i am trying to capture the output from the sorted file i m just getting only the Trailer record, using below code.

Could you please suggest me how to capture the output i.e sorted file which is having both Header and Footer.

Code:
echo $H; sed '1d;$d' input_file | sort -t'|' -k1.15,1.17; echo $T > output_sorted_file

Basically my output should be a sorted file with Header and Footer.

Thanks in Advance !
-Sasi
# 5  
Old 07-14-2011
There is 3 commands producing output so you would have to redirect 3 times:
Code:
echo $H > newfile; sed '1d;$d' infile| sort -t'|' -k1.15,1.17 >> newfile; echo $T >> newfile

You could alternatively redirect stdout to a file before starting the commands above, then reset it to your terminal:
Code:
H=$(head -1 infile)
T=$(tail -1 infile)
exec 1> newfile        careful, all following output to stdout will now be redirected into that file "newfile" until reset!
echo $H; sed '1d;$d' infile| sort -t'|' -k1.15,1.17; echo $T
exec 1> /dev/tty       resetting stdout to your terminal

Check your file "newfile" now.
This User Gave Thanks to zaxxon For This Post:
sasikari (07-14-2011)
# 6  
Old 07-14-2011
Thanks for the reply and its working fine now in UNIX.

As my design is changed to handle the same logic in PERL script ,Could someone explain how to handle the same logic in PERL code.

Regards
-Sasi
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk issue splitting a fixed-width file containing line feed in data pchang Shell Programming and Scripting 8 02-17-2017 04:34 PM
How to sum up data in fixed width file with decimal point? patricjemmy6 Post Here to Contact Site Administrators and Moderators 1 02-24-2015 07:51 PM
Eliminate Header and footer from EBCDIC file abhilashnair UNIX for Dummies Questions & Answers 4 12-18-2014 05:48 AM
Alter Fixed Width File vinus Shell Programming and Scripting 6 08-27-2014 12:30 PM
Length of a fixed width file Amrutha24 UNIX for Dummies Questions & Answers 2 06-19-2013 02:54 AM
File Row Line Count without Header Footer gagan8877 UNIX for Dummies Questions & Answers 7 05-02-2013 02:29 PM
Is there a way to append both at header and footer of a file jediwannabe Shell Programming and Scripting 3 02-28-2013 06:57 AM
Removing header or footer from file sridhardwh Shell Programming and Scripting 5 06-04-2012 06:43 AM
Add header and footer with record count in footer itsranjan Shell Programming and Scripting 1 03-25-2012 12:45 AM
sort a report file having header and footer suryanarayana Shell Programming and Scripting 4 11-25-2011 10:48 PM
Delete header row and reformat from tab delimited to fixed width chumsky UNIX for Dummies Questions & Answers 4 07-06-2011 04:22 AM
Comparing two fixed width file anshul_er Shell Programming and Scripting 10 05-19-2011 01:59 AM
Fixed-Width file from Oracle Amit.Sagpariya Shell Programming and Scripting 5 04-20-2010 08:44 AM
Total of lines w/out header and footer incude for a file gzs553 Shell Programming and Scripting 1 11-16-2006 07:42 PM
Need to Chop Header and Footer record from input file coolbudy Shell Programming and Scripting 4 08-09-2005 01:26 PM