0

I have some code I'm using with Windows and SED to give me the first set of eight characters in a file name that keeps giving me the second set only that I cannot figure out what I'm doing wrong.

My Code:

echo JiggySauce_20161208_21325005_Meat.txt | sed -r "s/.*_([0-9]*)_.*/\1/g"

Addition Example (so regex per underbar delimiters won't always work):

echo JiggySauce_Mustard_Mayo_20161208_21325005_Meat.txt | sed -r "s/.*_([0-9]*)_.*/\1/g"

I keep getting this wrong result (at least not what I need):

21325005

My expected result:

20161208

I could even live with (preferrably not but could work with that I suppose):

20161208_21325005

Please help me with this if you have an answer as I'm at a standstill looking dumb and stumped over here like UHHH....

SlickNutz
  • 149
  • 5
  • 2
    Probably related to sed pattern matching being greedy. Have a look at http://stackoverflow.com/questions/1103149/non-greedy-regex-matching-in-sed – John3136 Dec 09 '16 at 03:11
  • I see that you are interested in a `sed` solution, specifically, but it might still be of interest that `grep -o` would do nicely. – Michael Vehrs Dec 09 '16 at 13:49
  • @MichaelVehrs Add an answer for Grep if you have one that works, I can use Grep as well. – SlickNutz Dec 09 '16 at 14:02

2 Answers2

1

With GNU sed:

echo JiggySauce_20161208_21325005_Meat.txt | sed -r 's/^[^_]*_([^_]*).*/\1/'

Output:

20161208

Post Initial Answer Update:

I suggest: sed -r 's/[^0-9]*([0-9]{8}).*/\1/' Cyrus

Output:

20161208

See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
  • This works for my one example but I forgot to mention that the file names may have other name parts with additional underbars though so for example I also have `JiggySauce_Mustard_Mayo_20161208_21325005_Meat.txt` where that wouldn't work. I give you plus one regardless but will update my question with this detail I was missing. I look forward to a working answer from you still if you have one... I will glady accept if you do... Thank you much regardless though. – SlickNutz Dec 09 '16 at 03:32
  • I suggest: `sed -r 's/[^0-9]*([0-9]{8}).*/\1/'` – Cyrus Dec 09 '16 at 04:22
  • That is what I needed... Thank you.... Please update your answer with this when you get a chance... this is the answer I need. I will keep an eye on it an accept once you update your answer to include that syntax. – SlickNutz Dec 09 '16 at 04:45
1

Using grep:

echo JiggySauce_20161208_21325005_Meat.txt | grep -Eo '[0-9]+' | head -1

or

echo JiggySauce_20161208_21325005_Meat.txt | tr '_' '\n' | grep -m1 -Eo '[0-9]+'
Michael Vehrs
  • 3,089
  • 9
  • 10