0

I have a file with a patter like this:

1 1 1 2 0 1 0.5
1 2 2 2 0 2 0.5
2 1 1 1 0 1 0.25
2 1 2 2 0 2 0.5
2 3 3 3 0 3 0.25

I want to remove a line if another line in the file has the same last two entries. In the example above this means that line 4 should be removed:

1 1 1 2 0 1 0.5
1 2 2 2 0 2 0.5
2 1 1 1 0 1 0.25
2 3 3 3 0 3 0.25

I can't get my head around how I can do this via the command line or with a simple awk/sed script. Any help is greatly appreciated!

2 Answers 2

3
awk '!a[$NF,$(NF-1)]++' file

Make an array and check it hasn't already been populated with the last two fields.

Sign up to request clarification or add additional context in comments.

Comments

1

try:

 sort -k 6 f1.txt  | uniq -f 5

assuming the original order of the lines doesn't matters.

2 Comments

Thanks for the quick reply! I have only one file though. I want to remove the line in that file.
Sorry, misunderstood. Try new solution if order of lines doesn't matters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.