-1

I want to get a date from a file name. This works using this code:

for filename in OH/*
do
    echo $filename |grep -Eo '[[:digit:]]{14}'
done;

Now i want to save the result to a variable like this:

for filename in OH/*
do
    result=$($filename |grep -Eo '[[:digit:]]{14}')
    echo $result
done;

But i get 2 empty lines printed. What am i missing there?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
LaaKii
  • 1

2 Answers2

4

You dropped the echo:

for filename in OH/*
do
    result=$(echo $filename | grep -Eo '[[:digit:]]{14}')
    echo "$result"
done;

or better yet,

for filename in OH/*
do
    result=$(printf %s "$filename" |grep -Eo '[[:digit:]]{14}')
    echo "$result"
done;

or

for filename in OH/*
do
    result=$(grep -Eo '[[:digit:]]{14}' <<<"$filename")
    echo "$result"
done;
Stephen Kitt
  • 434,908
  • ${filename:$((-21)):-7} (in bash) would also pick out the date stamp, if the file name suffix is always the same length. – Kusalananda Feb 02 '18 at 10:37
  • 1
    It isn't always the same length. Thanks anyway! – LaaKii Feb 02 '18 at 10:38
  • 1
    @Kusalananda, ${filename: -21:-7} is enough to disambiguate it from ${par:-} (space between the : and -). And actually that's an arithmetic context anyway, so you could do even ${filename:0-3*7:-7} without the $(( )). – ilkkachu Feb 02 '18 at 10:53
  • @ilkkachu Thanks! I was scratching my head over that, but not for long enough it seems :-) – Kusalananda Feb 02 '18 at 10:57
  • Why not grep -Eo $filename '[[:digit:]]{14}'? Seems more portable than using <<< . – rien333 Feb 02 '18 at 12:00
  • 1
    rien333, that would look for the pattern $filename (or rather, the pattern corresponding to the value of the filename shell variable) in a file named [[:digit:]]{14}... – Stephen Kitt Feb 02 '18 at 12:39
  • There is a clear chance that some other files will match the glob OH/* but not the grep regex. In such case, a NULL result will be printed. Avoid with `[ "$result" ] && echo "$result" –  Feb 02 '18 at 19:19
  • Quoting "$result" seems reasonable. Edit done. –  Feb 02 '18 at 19:21
  • @isaac I’m not sure what quoting buys you here, at least with an empty result. Care to explain? – Stephen Kitt Feb 02 '18 at 19:47
  • No, it is not related to the empty (NULL) comment. That is why it is a separate comment. It is related to "quoting your expansions" as a reasonable good coding practice. I assumed you know it, that's why I directly did the edit. Better now? :-) –  Feb 02 '18 at 19:54
  • @isaac right that makes sense, thanks for the edit. “Quoting "$result" seems reasonable” is the sort of thing I would write in the edit comment, not as a post comment ;-). – Stephen Kitt Feb 02 '18 at 20:08
0

You are missing and echo inside the Command execution expansion, but better to use printf:

result=$(echo "$filename" | grep -Eo '[[:digit:]]{14}')

result=$(printf '%s' "$filename" | grep -Eo '[[:digit:]]{14}')

Of course, there is the chance that result is NULL if the file does not match the grep regex, for that we need a test:

#!/bin/bash 
for filename in OH/*
do
    result=$(printf '%s' "$filename" | grep -Eo '[[:digit:]]{14}')
    [ "$result" ] && echo "$result"
done;