4

I need to print all the lines containing non numeric characters using regex in sed. The input is a csv, and some of it's lines has text and numbers. I'm interested in printing only those without numbers. This is what I tried:

sed -E -n '/^\D*$/p' direcciones.csv

Why it doesn't find anything?

  • Printing all the lines containing non numeric characters != printing those [lines] without numbers. The former will not print an empty line while the latter would print it. Which do you want? – Ed Morton Jul 16 '19 at 5:14
10

@steeldriver already explained why your attempt didn't work (should work with GNU sed, though).

But why not keep it simple? Printing all lines with only non-numeric characters is the same as dropping all lines with numeric characters:

sed '/[0-9]/d' direcciones.csv

Easier to write and easier to read, isn't it?

  • wrt Printing all lines with only non-numeric characters is the same as dropping all lines with numeric characters - not quite, consider lines that have no characters (except the terminating newline of course) – Ed Morton Jul 16 '19 at 5:08
  • Maybe a matter of language precision. "You are allowed to pass the border with only toll free products" -- are you allowed to pass it without any products? Fortunally, regexp is more precise: /^[^0-9]*$/ is linewise the same as [0-9]. – Philippos Jul 16 '19 at 9:39
6

Your implementation of sed likely doesn't support the PCRE extension \D (even with the -E extended regular expression switch) . You should be able to use [^[:digit:]] instead:

$ printf 'foo,bar,baz\nfoo,123,baz\n' | sed -n '/^[^[:digit:]]*$/p'
foo,bar,baz
  • AFAIK no sed supports \d or \D as they're PCRE escape sequences and no sed supports PCREs. – Ed Morton Jul 16 '19 at 5:11
  • 1
    @EdMorton I suspected that was the case - but I was hesitant to assert it (especially since GNU sed does appear to support \s and \S) – steeldriver Jul 16 '19 at 11:20
  • Right, I was a little surprised when I noticed but GNU tools support \s, \S, \w, and \W in all REs (BREs, EREs, or PCREs) and GNU grep supports PCREs which include \d and \D but neither sed nor awk support PCREs and so don't support \d and \D. – Ed Morton Jul 16 '19 at 12:17
4

Are you restricted to sed only or can you use grep?

grep -v '[0-9]' direcciones.csv
  • 2
    That's not "all lines that don't have numbers" like the title says (and the sed attempt is trying to do), but all lines that are not entirely digits. You'd need grep -v '[0-9]' direcciones.csv instead – Philippos Jul 15 '19 at 12:55
  • 1
    ...or grep '^[^0-9]*$' – ilkkachu Jul 15 '19 at 13:45
  • 1
    @pLumo: The pattern you cite is suppose to mean "lines consisting only of non-digits (\D). The opposite is not "lines consisting of only digits", but "lines containing at least one digit" – Philippos Jul 15 '19 at 13:45
  • I need to drink more coffeee... thanks for insisting :-p – pLumo Jul 15 '19 at 13:47

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.