awk - how do i get the last row of a group


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - how do i get the last row of a group
# 1  
Old 05-13-2013
awk - how do i get the last row of a group

How do i print the last record of a group in a file ?

For example, I have a file like this :
Code:
cat txt
cucm1,location1,1,2,3
cucm2,location1,3,4,5
cucm1,location1,10,20,30
cucm2,location2,30,40,50

I am expecting a command that would print the last record of the group.

For example,
Code:
awk -F, '!a[$2$3]++' txt

prints the first record, where $2 and $3 matches.
My requirement is to print the last records.
i.e
Code:
cucm1,location1,10,20,30
cucm2,location2,30,40,50

Thanks

Last edited by Franklin52; 05-13-2013 at 11:04 AM.. Reason: code tags
# 2  
Old 05-13-2013
try

Code:
 
awk -F"," '{A[$1]=$0}END{for (i in A){print A[i]}}'  filename

# 3  
Old 05-13-2013
Wonderful. Thanks. It works.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter Row Based On Max Column Value After Group BY

Hello Team, Need your expertise on following: Here is the set of data: C1|4|C1SP1|A1|C1BP1|T1 C1|4|C1SP2|A1|C1BP2|T2 C2|3|C2SP1|A2|C2BP1|T2 C3|3|C3SP1|A3|C3BP1|T2 C2|2|C2SP2|A2|C2BP2|T1 I need to filter above date base on following two steps: 1. Group them by column 1 and 4 2.... (12 Replies)
Discussion started by: angshuman
12 Replies

2. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

3. Shell Programming and Scripting

Add a Group ID to each row

I have a sample data like this: user1 1001 role1 user2 1002 role1 role2 user3 1003 role1 role2 role3 And I need to convert it like this: 1,user1 (5 Replies)
Discussion started by: vskr72
5 Replies

4. Shell Programming and Scripting

awk get the row

somthing somthing A B C B F A B .... 1 2 3 4 5 6 7 .... 7 9 12 2 4 5 8 I want to get the row which is the same row as B. This this the 2 4 7 9 2 8 (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

7. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

8. Shell Programming and Scripting

AWK: row number NR

Hi I've file1 as: after I read all rows with awk, I need to change some of them. I mean, for example if the last row is zero then change row number 4 in zero too. So I'd like to refers each row as a vector and change its value accordly some conditions. I know that NR keep just the "current"... (2 Replies)
Discussion started by: Dedalus
2 Replies

9. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies

10. Shell Programming and Scripting

Get value of last row and 6 column from awk

I want to get value of last row and 6 column from awk. Below is the format of my file. And RED one is my desired value. Actaully this stats usally update after every 1 hour so i want that every time i run the script i get the latest value. Ending time - 01:00:58 HOURLY CALL ATTEMPTS... (4 Replies)
Discussion started by: wakhan
4 Replies
Login or Register to Ask a Question