|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Hi all I have a file which looks like this Code:
1234|1|Jon|some text|some text 1234|2|Jon|some text|some text 3453|5|Jon|some text|some text 6533|2|Kate|some text|some text 4567|3|Chris|some text|some text 4567|4|Maggie|some text|some text 8764|6|Maggie|some text|some text My third column is my KEY and I want to only print lines of the file if the KEY has been printed more than once. So basically any unique entry for column three can be deleted. Code:
So the output would look like this 1234|1|Jon|some text|some text 1234|2|Jon|some text|some text 3453|5|Jon|some text|some text 4567|4|Maggie|some text|some text 8764|6|Maggie|some text|some text Can you please help me?
Last edited by Scrutinizer; 06-10-2012 at 11:59 PM.. Reason: code tags instead of code tags |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Try... Code:
$ cat file1
1234|1|Jon|some text|some text
1234|2|Jon|some text|some text
3453|5|Jon|some text|some text
6533|2|Kate|some text|some text
4567|3|Chris|some text|some text
4567|4|Maggie|some text|some text
8764|6|Maggie|some text|some text
$ awk 'NR==FNR{a[$3]++;next}a[$3]>1' FS='|' file1 file1
1234|1|Jon|some text|some text
1234|2|Jon|some text|some text
3453|5|Jon|some text|some text
4567|4|Maggie|some text|some text
8764|6|Maggie|some text|some text
$ |
| The Following User Says Thank You to Ygor For This Useful Post: | ||
A-V (06-12-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
fortunately it doesn't do any anything on my file
![]() so even putting into a file it returns an empty file oooppsssss ... my mistake ![]() I only put the file1 once why we need to put it twice? is it for comparison? Thanks for your help ---------- Post updated at 08:31 AM ---------- Previous update was at 08:16 AM ---------- i tested it on my documents somehow, it does not delete all the single lines so I do steel have unique data on the other hand it deletes one row from the non-unique ones as well so if i have two james on file one, in output i have 1 james only any suggestion? Last edited by A-V; 06-12-2012 at 09:23 AM.. |
|
#4
|
||||
|
||||
|
awk
Hi, Try this one, Code:
awk 'BEGIN{FS="|";}{a[$3]++;if(a[$3]==2)print v[$3] ORS $0;if(a[$3]>2)print;v[$3]=$0;}' fileCheers, Ranga
|
| The Following User Says Thank You to rangarasan For This Useful Post: | ||
A-V (06-12-2012) | ||
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Dear Ranga
its worked as a charm Thank you so much Cheers A-V |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Delete rows with unique value for specific column
Hi Ranga, Good One but can you please compeltly how this logic works? Code:
awk 'BEGIN{FS="|";}{a[$3]++;if(a[$3]==2)print v[$3] ORS $0;if(a[$3]>2)print;v[$3]=$0;}' fileThanks Krsna |
| The Following User Says Thank You to krsnadasa For This Useful Post: | ||
A-V (06-14-2012) | ||
| Sponsored Links | |
|
|
#7
|
||||
|
||||
|
awk
Quote:
Code:
a[$3]++; - Store the no of repeat counts with name as a key. v[$3]=$0; - store the previous line if(a[$3]==2) - If repeat count is more than one(must be 2), then print previous line(first occurence) and current line(second occurence). if(a[$3]>2) - Just print the current line. Hope i explained clearly. Cheers, Ranga
|
| The Following User Says Thank You to rangarasan For This Useful Post: | ||
A-V (06-14-2012) | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| delete a row with a specific value at a certain column | kylle345 | Shell Programming and Scripting | 3 | 03-30-2012 12:02 AM |
| Delete all rows that contain a specific string (text) | evelibertine | UNIX for Dummies Questions & Answers | 9 | 06-16-2011 11:20 PM |
| Delete a specific column using vi editor? | fnebiolo | UNIX for Dummies Questions & Answers | 3 | 10-25-2010 03:28 AM |
| Print rows, having pattern in specific column... | admax | Shell Programming and Scripting | 25 | 10-06-2009 06:14 AM |
| how to delete duplicate rows based on last column | reva | Shell Programming and Scripting | 16 | 09-01-2009 09:12 AM |
|
|