0

I've read How do I delete the first n lines of an ascii file using shell commands?, it is helpful. However I've a file something as below (please consider 2 columns as 2 different files):

1 4
1 4
1 4
1 4
1 4
3 5
3 5
3 5
3 5
3 5
7 5
7 5
7 5
7 5
7 5

I need to delete line 2-5 then 7-10 and so on (Valid outputs are 1, 3, 7 and 4, 5, 5).

I know number of lines for which pattern (number) repeats. However I don't know if everytime it would follow same pattern for e.g. here its 1, 3, 7 next file can have 4, 6, 1 or 4, 5, 5 so I need to make it number of lines based than grepping the values.

Can anyone give me a pointer how would I delete lines repetitively?

  • is there a possibility that your file may contain more than 15 lines? – user1678213 Jan 19 '13 at 15:15
3

It sounds like you're looking for uniq.

Or, from the comments:

sed '2,5d;7,10d;12,$d'
  • Thanks however uniq fails in case of 4, 5, 5 (all repeated 5 times each). This is why I was looking for number of lines based solution – wisemonkey Jan 18 '13 at 21:25
  • @wisemonkey, I guess I don't properly understand the question. Maybe you should add more examples or try describing it another way. – Samuel Edwin Ward Jan 18 '13 at 21:26
  • My bad, please have a look at modified question. Hopefully second sequence makes it easier (for now I'm going to use uniq and then add extra entry if required) – wisemonkey Jan 18 '13 at 21:34
  • 1
    @wisemonkey, is sed '2,5d;7,10d;12,$d' what you're looking for? – Samuel Edwin Ward Jan 18 '13 at 21:46
  • exactly but I didn't know I can cascade them like that, awesome thanks a lot :D – wisemonkey Jan 18 '13 at 21:48
0

If what you are really asking is output every nth line of a file:

cat file | perl -ne '$. % 5 or print'

Possibly:

cat file | perl -ne '($.-1) % 5 or print'
-1

use following command

cat filename | sort | uniq > filename2
  • How does sort and uniq help in this case? Your solution would fail on the given example itself. Since the output is expected to be 4,5,5, however, uniq would eliminate the second 5. – darnir May 15 '14 at 11:07
  • @darnir Code tested on the given file and the output is as expected i.e. 1 4 3 5 7 5 – newbie17 May 19 '14 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.