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?

1
  • 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
    Commented Jul 16, 2019 at 5:14

3 Answers 3

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?

2
  • 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
    Commented Jul 16, 2019 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
    Commented Jul 16, 2019 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
3
  • AFAIK no sed supports \d or \D as they're PCRE escape sequences and no sed supports PCREs.
    – Ed Morton
    Commented Jul 16, 2019 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) Commented Jul 16, 2019 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
    Commented Jul 16, 2019 at 12:17
5

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

grep -v '[0-9]' direcciones.csv
4
  • 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
    Commented Jul 15, 2019 at 12:55
  • 1
    ...or grep '^[^0-9]*$'
    – ilkkachu
    Commented Jul 15, 2019 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
    Commented Jul 15, 2019 at 13:45
  • I need to drink more coffeee... thanks for insisting :-p
    – pLumo
    Commented Jul 15, 2019 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .