Deleting n number of consecutive occurrences of a pattern
Hello All,
I want to delete a particular number of consecutive occurrences of a pattern from the file using awk. Please help me with the same. Example of the file contents 0000 0010 0011 0000 0000 0000 0000 0000 1111 1111 0010 0000 Now I want to delete only the block where 0000 has repeated 5 times consecutively and keep other 0000's unchanged. How can i do this using awk? Thanks in advance, Thirumala |
So you want, what have you attempted ?.
You make the effort, we'll help when you run into trouble. |
Hey syg00,
I have tried the below command cat temp | awk 'N&&sub(PAT,REPL){N--};1' N=291 PAT="0000" REPL="" > temp1 cat temp1 | sed '/^$/d' > temp2 This command deletes first 291 occurrences but I want to delete the 291 consecutive occurrences. Thanks, Thirumala |
Try this:
When the input line matches the pattern, remember the line in an array and count down. If counter is 0, throw the array away and set the counter back to N. When it doesn't match the pattern: - if the array isn't empty, less than N patterns were in a row, so write the array out. Clear the array. Set the counter back to N. - print the current line I wonder if it can be done with other commands. |
Another thing to consider would be, what if there are more than 5 in a row? Do you delete if it is 6? Or only if another 5, ie. 10?
|
It should not replace if occurrences are more than n. And it should replace only if next set of occurrences are n again.
|
Nope, we are not going to write it for you.
You have been given some hints - incorporate them in your code. The countdown is a good idea, use it to also test if the current record is equal to the previous. |
Quote:
Code:
#!/usr/bin/awk -f Quote:
|
@berndbausch - just remember that now this user may expect to be told answers without doing any work in the future too :(
But, as you have let the cat out of the bag, here are 2 points of interest: 1. What happens if the last 3 entries in the file are the pattern? 2. If you rethink your use of N, you could reduce it to only being needed once outside the definition ;) (hint: consider ix values) |
Hello All,
Thanks for the help. This is the first time i am using awk so took more help.:) And rest assured that i will not expect any ready answers from you guys. Thanks, Thirumala |
Quote:
Well, whenI have a little more time I may do the polishing just to prove my value :) |
Quote:
|
Quote:
Quote:
|
Not all awk versions print a
Code:
for (i in array) Because the order is to be kept, we can store it in a string as well Code:
awk ' |
I think some of you might be getting a little too carried away with the order stuff, try and remember what is being stored in the array, ie. it is only the same pattern (0000), so really
order here is pretty irrelevant ;) |
All times are GMT -5. The time now is 04:42 PM. |