0

I'd like to grab the digits in a string like so :

 "sample_2341-43-11.txt"   to   2341-43-11

And so I tried the following command:

echo "sample_2341-43-11.txt" | sed -n -r 's|[0-9]{4}\-[0-9]{2}\-[0-9]{2}|\1|p'

I saw this answer, which is where I got the idea. Use sed to grab a string, but it doesn't work on my machine:

  • it gives an error "illegal option -r".
  • it doesn't like the \1, either.

I'm using sed on MacOSX yosemite.

Is this the easiest way to extract that information from the file name?

Community
  • 1
  • 1
makansij
  • 9,303
  • 37
  • 105
  • 183

5 Answers5

2

You need to set your grouping and match the rest of the line to remove it with the group. Also the - does not need to be escaped. And the -n will inhibit the output (It just returns exit level for script conditionals).

echo "sample_2341-43-11.txt" | sed -r 's/^.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*$/\1/'
Zen Momentum
  • 133
  • 1
  • 6
  • It will not work on MacOSX yosemite! see: http://stackoverflow.com/questions/12178924/os-x-sed-e-doesnt-accept-extended-regular-expressions – Nir Alfasi Sep 18 '15 at 01:51
2

Enhanced regular expressions are not supported in the Mac version of sed.

You can use grep instead:

echo "sample_2341-43-11.txt" | grep -Eo "((\d+|-)+)"

OUTPUT

2341-43-11
Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0
echo "one1sample_2341-43-11.txt" \
 | sed 's/[^[:digit:]-]\{1,\}/ /g;s/ \{1,\}/ /g;s/^ //;s/ $//'

1 2341-43-11
  • Extract all numbers(digit) completed with - (thus allow here --12 but can be easily treated)
  • posix compliant
  • all number of the line are on same line (if several) separate by a space character (could be changed to new line if wanted)
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
0

You can try this ways also

sed 's/[^_]\+_\([^.]\+\).*/\1/' <<< sample_2341-43-11.txt

OutPut:

2341-43-11

Explanation:

[^_]\+       - Match the content untile _ ( sample_)
\([^.]\+\)   - Match the content until . and capture the pattern (2341-43-11)
.*           - Discard remaining character (.txt)
Kalanidhi
  • 4,902
  • 27
  • 42
-1

You can go with what the poster above said. Well, making use of this

pattern "\d+-\d+-\d+" would match what you are looking for. See demo here

https://regex101.com/r/kO2cZ1/3

james jelo4kul
  • 839
  • 4
  • 17