Cut the final line of each sorted value

Hi
I have file like the below mentioned example. Need some kind of unix script to achive the output.
Input:

PA|23|2013-01-23|252
PA|23|2013-01-23|264
PA|25|2013-01-22|200
PA|23|2013-01-27|156
PA|23|2013-01-27|002
PA|23|2013-01-27|014

Output:

PA|23|2013-01-23|264
PA|25|2013-01-22|200
PA|23|2013-01-27|156
PA|23|2013-01-27|014

I am not seeing any sort order for the given input where the 1st line of your input would be the final line of a sorted value. Please explain your sort order you are using.

i am using the below sorting order.

sort -k1b,1 -k2b,2 -k3b,3 -k4b,4

That would produce

PA|23|2013-01-23|252
PA|23|2013-01-23|264
PA|23|2013-01-27|002
PA|23|2013-01-27|014
PA|23|2013-01-27|156
PA|25|2013-01-22|200

I still don't see any relation to the required OP!! :confused:

Since your sort command doesn't specify a field separator character and since there are no blanks in your input file, the command:

sort

will produce exactly the same results. And, as I said before and as PikK45 has also noted, using this sort order and deleting the last line of the sorted results doesn't even come close to the output you said you expect.

Even if I assume that there are multiple concatenated sets of sorted lines in your input file, the output you said you expect still doesn't even come close to what you would get by deleting the last line of each sorted set of lines in your input.

Anshaa,

what was the logic behind your output. How did you separate those output lines. then only we can help to you.

My inital input file was somethign like below :
INPUT

SATURDAY|02-JAN-13|2|ADD|165242.56
SATURDAY|02-JAN-13|2|SUB|1602446.2
SATURDAY|02-JAN-13|2|MULT|109762.97
SATURDAY|04-JAN-13|4|ADD|299754.28
SATURDAY|04-JAN-13|4|SUB|3196764.19
SATURDAY|04-JAN-13|4|MULT|224014.41
SATURDAY|07-JAN-13|7|ADD|68130.48
SATURDAY|07-JAN-13|7|SUB|634547.65
SATURDAY|07-JAN-13|7|MULT|42885.53
SATURDAY|08-JAN-13|8|ADD|236907.27
SATURDAY|08-JAN-13|8|SUB|2495910.06
SATURDAY|08-JAN-13|8|MULT|173774.33
SATURDAY|08-JAN-13|8|ADD|236925.26
SATURDAY|08-JAN-13|8|SUB|2496364.66
SATURDAY|08-JAN-13|8|MULT|173805.1
SATURDAY|07-JAN-13|7|ADD|68362.5
SATURDAY|07-JAN-13|7|SUB|635450.66
SATURDAY|07-JAN-13|7|MULT|42927.81
SATURDAY|07-JAN-13|7|ADD|68362.5
SATURDAY|07-JAN-13|7|SUB|635686.14

after using "sort" of this file i got the below file.

SATURDAY|02-JAN-13|2|ADD|165242.56
SATURDAY|02-JAN-13|2|SUB|1602446.2
SATURDAY|02-JAN-13|2|MULT|109762.97
SATURDAY|04-JAN-13|4|ADD|299754.28
SATURDAY|04-JAN-13|4|SUB|3196764.19
SATURDAY|04-JAN-13|4|MULT|224014.41
SATURDAY|07-JAN-13|7|ADD|68130.48
SATURDAY|07-JAN-13|7|ADD|68362.5
SATURDAY|07-JAN-13|7|ADD|68362.5
SATURDAY|07-JAN-13|7|SUB|634547.65
SATURDAY|07-JAN-13|7|SUB|635450.66
SATURDAY|07-JAN-13|7|SUB|635686.14
SATURDAY|07-JAN-13|7|MULT|42885.53
SATURDAY|07-JAN-13|7|MULT|42927.81
SATURDAY|08-JAN-13|8|ADD|236907.27
SATURDAY|08-JAN-13|8|ADD|236925.26
SATURDAY|08-JAN-13|8|SUB|2495910.06
SATURDAY|08-JAN-13|8|SUB|2496364.66
SATURDAY|08-JAN-13|8|MULT|173774.33
SATURDAY|08-JAN-13|8|MULT|173805.1

But after this i need something like the below format.

SATURDAY|02-JAN-13|2|ADD|165242.56
SATURDAY|02-JAN-13|2|SUB|1602446.2
SATURDAY|02-JAN-13|2|MULT|109762.97
SATURDAY|04-JAN-13|4|ADD|299754.28
SATURDAY|04-JAN-13|4|SUB|3196764.19
SATURDAY|04-JAN-13|4|MULT|224014.41
SATURDAY|07-JAN-13|7|ADD|68362.5
SATURDAY|07-JAN-13|7|SUB|635686.14
SATURDAY|07-JAN-13|7|MULT|42927.81
SATURDAY|08-JAN-13|8|ADD|236925.26
SATURDAY|08-JAN-13|8|SUB|2496364.66
SATURDAY|08-JAN-13|8|MULT|173805.1

I need the last row from each sorted value(combination of first 4 columns)

Again, your input data and the sorted input data do not match. If you had sorted the input on the first four columns (assuming | is the field separator), SUB would follow MULT in your sorted when the first three fields match.

The following does what you requested, but (as described above) it doesn't match the output you said you needed:

sort INPUT | awk '
match($0, /.*[|]/) == 1 {
        k = substr($0, 1, RLENGTH)
        if(lk != k) {
                if(ll) print ll
                lk = k
        }
        ll = $0
}
END {   print ll}'

which produces the output:

SATURDAY|02-JAN-13|2|ADD|165242.56
SATURDAY|02-JAN-13|2|MULT|109762.97
SATURDAY|02-JAN-13|2|SUB|1602446.2
SATURDAY|04-JAN-13|4|ADD|299754.28
SATURDAY|04-JAN-13|4|MULT|224014.41
SATURDAY|04-JAN-13|4|SUB|3196764.19
SATURDAY|07-JAN-13|7|ADD|68362.5
SATURDAY|07-JAN-13|7|MULT|42927.81
SATURDAY|07-JAN-13|7|SUB|635686.14
SATURDAY|08-JAN-13|8|ADD|236925.26
SATURDAY|08-JAN-13|8|MULT|173805.1
SATURDAY|08-JAN-13|8|SUB|2496364.66
1 Like

Sorry My bad.
Thanks the solution works great.
I used nawk, instead of awk.