Maybe an odd question, but I’m attempting to grep the output of a command to select just the matching word and not the line. This word also has a wildcard in it.
git log --format=%aD <file> | tail -1 | grep -oh 201
The first and second sections of the command check the git log for a file and grabs the line pertaining to the date and time of creation. I’m attempting to write a bash script that does something with the year it was created, so I need to grab just that one word (the year).
Looking at the grep documentation, -o specifically prints the matching word (and -h suppresses filenames). I can’t find anything that allows for matching the rest of the word that it’s matching, though (I could just be spacing).
So the output of that previous command is:
201
And I need it to be (as an example):
2017
Help would be much appreciated!
Advertisement
Answer
You can use .
as a wildcard character:
$ echo 'before2017after' | grep -o '201.' 2017
Or, better yet, specify that the fourth character be a digit:
$ echo 'before2017after' | grep -o '201[[:digit:]]' 2017
Notes:
Since you are getting input from stdin, there are no filenames. Consequently, in this case,
-h
changes nothing.[[:digit:]]
is a unicode-safe way of specifying a digit.