2

I found a similar question:

But in this case I want to check not the total length, but the number of digits in the line. For example, out of this input:

cdc85e24-b9e9-8802-080a-b84479e1ae82  
ekodeveloper0  
1795475824.1129747.1472396049615.ref  
1795475824.1129747.1472396049615  
967175540.1194446.1472407271491  
wangxiuyan552  
jveazey  
xoloki  
whelee  
matthauck  
patel3.anirudh  
mischa.salle  

How can I delete all the lines with more than N digits in them?

  • Post your desired output as well as your input please. – Wildcard Nov 1 '16 at 0:26
1

It's easier to only keep lines having X or more numbers. Here is an example of that where X is 13:

sed -n 's/[0-9]/&/13p' input.txt > output.txt

However, you can modify this to do the reverse. Here is a command that will only keep lines having fewer than 13 numbers:

sed -n -e 's/[0-9]/&/13;t' -e 'p' input.txt > output.txt

Explanation - print only lines with 13+ numbers

sed -n 's/[0-9]/&/13p' input.txt > output.txt

The & symbol in replacement text means "everything that was matched."

The number flag on the s command (after the /find/replace/ part) means only replace that instance of the search pattern.

The p flag on the s command means "print if a substitution was made."

The -n switch, of course, is to suppress Sed's default "print" action.

Explanation - print only lines with <13 numbers

sed -n -e 's/[0-9]/&/13;t' -e 'p' input.txt > output.txt

The -e switch is "execute"; it just passes a command. We need it here since we are passing more than one.

The t command skips the remaining commands if the last s command performed a substitution.

The p command prints.

  • Thanks. Trying it out right now. I forgot to mention that the thing I linked has nothing to do with this current request. – silc Nov 1 '16 at 0:35
  • @silc, okay; I saw the similarity anyway. Don't forget to "accept" the answer by clicking the checkmark at the left if it solved your question. – Wildcard Nov 1 '16 at 0:38
  • Where do I execute this in? "sed" unrecognized command in cmd.exe and perl command line. – silc Nov 1 '16 at 0:42
  • @silc, any shell. Bash, ksh, zsh, dash, any of them should work just fine. Not sure I get the question; you tagged this post "shell script." – Wildcard Nov 1 '16 at 0:45
  • I wasn't sure which program it could be done in. I'm trying to make it work in shell now. "The term "sed" is not recognized as the name of a cmdlet, function script file or operable program. Check spelling or path and try again. At line:1 char: 4 – silc Nov 1 '16 at 0:54
3

Since you tagged your question perl, once way to do it in perl would be to make use of the fact that perl's tr returns a count of the number of characters replaced; so for example (taking N = 13) you could do

perl -ne 'print unless tr/[0-9]/[0-9]/ > 13' file
ekodeveloper0
wangxiuyan552
jveazey
xoloki
whelee
matthauck
patel3.anirudh
mischa.salle

A possibly more conventional way would be to evaluate the regex match in a scalar context to get a count

perl -ne '$c = () = /\d/g; print unless $c > 13' file

See for example Is there a Perl shortcut to count the number of matches in a string?

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.