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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore Header and Footer and Sort the data in fixed width file
# 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:
# 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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

2. Post Here to Contact Site Administrators and Moderators

How to sum up data in fixed width file with decimal point?

HI Everyone, I have below source file AAA|NAME1|ADDRESS1|300.20 BBB|NAME2|ADDRESS2|400.31 CCC|NAME3|ADDRESS3|300.34 I have requirement where I need to sum up fourth field in above fixed width pipe delimited flat file. When I use below code, it gives me value 1001.00 But I am expecting... (1 Reply)
Discussion started by: patricjemmy6
1 Replies

3. UNIX for Dummies Questions & Answers

Eliminate Header and footer from EBCDIC file

Is there any command to eliminate Header and footer from EBCDIC file (4 Replies)
Discussion started by: abhilashnair
4 Replies

4. Shell Programming and Scripting

Is there a way to append both at header and footer of a file

currently I've a file Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('024', '024', 2, sysdate); Insert into CD_CARD_TYPE (CODE, DESCRIPTION, LAST_UPDATE_BY, LAST_UPDATE_DATE) Values ('032', '032', 2, sysdate); ........ is it... (3 Replies)
Discussion started by: jediwannabe
3 Replies

5. Shell Programming and Scripting

Removing header or footer from file

Hi Every one, what is the coomand to remove header or footer from a file. Please help me by providing command/syntax to remove header/footer from unix. Thanks in advance for all your support. (5 Replies)
Discussion started by: sridhardwh
5 Replies

6. Shell Programming and Scripting

Add header and footer with record count in footer

This is my file(Target.txt) name|age|locaction abc|23|del xyz|24|mum jkl|25|kol The file should be like this 1|03252012 1|name|age|location 2|abc|23|del 2|xyz|24|mum 2|jkl|25|kol 2|kkk|26|hyd 3|4 Column 1 is row indicator for row 1 and 2, column indicator is 1,for data rows... (1 Reply)
Discussion started by: itsranjan
1 Replies

7. Shell Programming and Scripting

sort a report file having header and footer

I am having report file with header and footer . The details in between header and footer are separated by a pipe charater. I want to sort the file by considering multiple columns in between header and footer. pls help (4 Replies)
Discussion started by: suryanarayana
4 Replies

8. UNIX for Dummies Questions & Answers

Delete header row and reformat from tab delimited to fixed width

Hello gurus, I have a file in a tab delimited format and a header row. I need a code to delete the header in the file, and convert the file to a fixed width format, with all the columns aligned. Below is a sample of the file:... (4 Replies)
Discussion started by: chumsky
4 Replies

9. Shell Programming and Scripting

Total of lines w/out header and footer incude for a file

I am trying to get a total number of tapes w/out headers or footers in a ERV file and append it to the file. For some reason I cannot get it to work. Any ideas? #!/bin/sh dat=`date +"%b%d_%Y"` + date +%b%d_%Y dat=Nov16_2006 tapemgr="/export/home/legato/tapemgr/rpts"... (1 Reply)
Discussion started by: gzs553
1 Replies

10. Shell Programming and Scripting

Need to Chop Header and Footer record from input file

Hi, I need to chope the header and footer record from an input file and make a new output file, please let me know how i can do it in unix.thanks. (4 Replies)
Discussion started by: coolbudy
4 Replies
Login or Register to Ask a Question