Sort without Header and Trailer

Hi ,
My UNIX system is SUN Solaris.
I am trying to do a simple thing as described below.
I have a PIPE delimited file that has header and trailer. So the file is something like below:

Test1.txt looks like something below:

field_data1|field_data2|and some more data --Header
  Field1|field2|....and so on -Main data
  T|some info --Trailer

I want to perform the things below in this file:

  1. Remove the trailer from Test1.txt
  2. Save the header from Test1.txt
  3. Sort the records in Test1.txt excluding header (and run some business operation already in place on Test1.txt) that will create a subset pipe delimited file say Test2.txt
  4. Sort Text2.txt by field 2 and 3
  5. Add the header from step2(above) in Test2.txt

So the output should be Test2.txt -sorted by field 2 and 3 and add the header from step2.

I would appreciate any advice and help.

Thanks

How about

{ head -1 ; head -n-1 | sort -t"|" -k2,3; } < Test1.txt

If happy, redirect result to a file.

With sed instead of GNU head

{
read header; printf "%s\n" "$header"
sed '$d' | sort -t\| -k2,3
} < Test1.txt > Test2.txt
1 Like
{ head -1 ; head -n-1 | sort -t"|" -k2,3; } < Test1.txt

Thanks for the reply. But the code is not working. Getting the error below:

head: Invalid "-n -1" option
usage: head [-n #] [-#] [filename...]

Thanks

---------- Post updated at 03:52 PM ---------- Previous update was at 03:50 PM ----------

{
read header; printf "%s\n" "$header"
sed '$d' | sort -t\| -k2,3
} < Test1.txt > Test2.txt

Thanks a bunch. The code above working fine for me.

I wanted to check if there is a way to output without header as well.

Thanks again.

[quote=saanvi1;302962264]

{ head -1 ; head -n-1 | sort -t"|" -k2,3; } [..]

I wanted to check if there is a way to output without header as well.


Try:
sed '1d;$d' Test1.txt | sort -t\| -k2,3 > Test2.txt
1 Like
tail -n +2 Test1.txt | sort -t\| -k2,3