Help with keep the first record display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with keep the first record display
# 1  
Old 07-09-2013
Help with keep the first record display

Input file:
Code:
10 TMP_12 asdg
13 TMP_12 asdg
123 TMP_12 pasf 
123 ASD qwe
224 AW4 asrfwe
524 AW4 2342
724 AW4 poimv
241 AW4 oiljs
.
.

Desired output file:
Code:
10 TMP_12 asdg
13 TMP_12 asdg
123 ASD qwe
224 AW4 asrfwe
.
.

Generate desired output file by following criteria:
1. If column 2 data between two consecutive row are different, print whatever it is;
2. If column 2 are same but column 3 are different between two consecutive row, print the first record find in particular column 2 data;
3. If column 2 and column 3 data are exactly the same between two consecutive row, print it out everything in those particular record;

Below is the command I try:
Code:
awk '!_[$2]++' infe
10 TMP_12 asdg
123 ASD qwe
224 AW4 asrfwe

Above command can't really handle when face criteria 3 as stated above Smilie

Thanks for any advice Smilie
# 2  
Old 07-09-2013
This is how far I can get with that terse input sample (and I'm sure I did not understand your clause 2.):
Code:
awk     '!_[$2]++
         _[$2] && T[$2,$3]
                                {T[$2,$3]++}
        ' file
10 TMP_12 asdg
13 TMP_12 asdg
123 ASD qwe
224 AW4 asrfwe

# 3  
Old 07-09-2013
Hi RudiC,

Thanks a lot for your suggestion.
Below is the example of Clause 2:
Code:
224 AW4 asrfwe
524 AW4 2342
724 AW4 poimv
241 AW4 oiljs

From the above 4 records, they all share the same column 2 data "AW4" but have different column 3 data. For this case, I prefer just keep only the first record.
Code:
224 AW4 asrfwe

Hope it makes the thing clear Smilie
Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

2. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

3. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

4. UNIX for Dummies Questions & Answers

Display latest record from file based on multiple columns combination

I have requirement to print latest record from file based on multiple columns combination. EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/16/2015 100004 03/16/2015 100005 001 EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/18/2015 140003 03/18/2015 140004 001 EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/18/2015 220006... (1 Reply)
Discussion started by: tmalik79
1 Replies

5. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

6. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

7. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

8. Shell Programming and Scripting

Retrieve the record using 2,2 lines and display the ouput in columnwise

This is my file ------------- Using index 8 for dump of table abd_det. (6101) Dumped 0 records. (13932) Using index 10 for dump of table abscc_det. (6101) Dumped 0 records. (13932) Using index 14 for dump of table absc_det. (6101) Dumped 254365 records. (13932) Using index 16 for dump of... (10 Replies)
Discussion started by: chakkaravarthy
10 Replies

9. Shell Programming and Scripting

Script to display record spanning over multiple lines

Following are the lines from /etc/sudoers.conf bob SPARC = (OP) ALL : SGI = (OP) ALL fred ALL = (DB) NOPASSWD: ALL ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\ /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM Could you please help me with shell/perl script to display the records with... (2 Replies)
Discussion started by: Ujan
2 Replies

10. UNIX for Dummies Questions & Answers

how to read record by record from a file in unix

Hi guys, i have a big file with the following format.This includes header(H),detail(D) and trailer(T) information in the file.My problem is i have to search for the character "6h" at 14 th and 15 th position in all the records .if it is there i have to write all those records into a... (1 Reply)
Discussion started by: raoscb
1 Replies
Login or Register to Ask a Question