Script to remove lines in a file with more than "x" instances of any character ?
Hi,
I'm looking for a script (bash, python, perl etc) or even a one liner (sed, awk etc) that can take a set of files and remove any line that has more than "x" instances of any character (case sensitive). I have been doing a lot of searching and can only come up with examples of how to remove blank lines, lines that start with a certain character or lines that contain a certain string. This will be used on a system running a Kubuntu derivative. As a very poor and basic example, I would like to take files that contain lines like: Code:
ABC123#() Code:
ABC123#() I hope that makes sense. I know this must be possible, but for the life of me I cannot find even an example that will lead me in the right direction or better yet a piece of code I can use. Thank you for taking a look at my post and I hope it's not me missing an obvious way of doing this. |
So are you telling it which characters to look for or only that it cannot contain more than 2 (for example) of any character?
|
it cannot contain more than 2 of any character.
AAab = keep (because case sensitive) AAAb = delete (because of 3 A's) AbCb = keep (only 2 chars the same) #b## = delete (because of 3 #'s) Thanks for the interest. |
Try
Code:
n=2 |
Code:
awk -vFS= '{ |
Nice one Ken :)
|
Quote:
Would it be possible to make it so I can: script.sh -n 2 -f *.txt and have it process all the files -f *.txt have the n input as the script is run as this number can change depending on the files processed modify the existing files or create new ones with the same name and delete the old ones ? I know I have a real cheek and am probably pushing my luck asking for that, but it is obvious that you could do this far easier than I could. Really nice simple solution, thank you so much for what you have given me. |
Quote:
I tried this but although I could see it running through the file line by line, it neither changed the file nor create a new file with only the required lines. Sorry |
So you asked for a solution and a few were provided and then when needing to have it run on a large amount of data you want someone to do the next step too?
Remember this is supposed to be a learning experience. What have you tried in the way of implementing the grep solution on multiple files? Not that I would recommend it as it may never finish but grep itself has a -r option for recursive looking. As for the awk: Quote:
|
Quote:
I see what you are saying and will work out the finer refinements myself Thanks for the pointing out the obvious I totally missed. And a big thank you to the people that provided me with a code snippet to build from. |
No probs ... just post when you get stuck :)
I would also suggest looking at something like: Code:
while read -r line |
Hi pissed_budgie,
In perl Code:
#!/usr/bin/perl Code:
perl test.pl file 2 |
Thanks for all the replies and code snippets, I can't believe how simple this turned out to be.
|
All times are GMT -5. The time now is 11:06 PM. |