Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to do something like this in bash:

grep ( date | awk '{print "2006-" $6}' ) /some/file/here

But that syntax is incorrect.

The goal is to grep /some/file/here for the pattern 2006-2011 where 2011 is the current year.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted
grep "$(date | awk '{print "2006-" $6}')" /some/file/here

"..." holds its contents as one argument (even if there's whitespace).

$(...) is for "command substitution", where the stdout from running the embedded command will be put into place on the original command-line. (Another syntax, `...`, is also common but much harder to nest.)

Easier:

grep "$(date +'2006-%Y')" /some/file/here

Here, you use date's ability to format the output arbitrarily.

Note that none of these match any year in the range 2006-2011, they match the literal string "2006-2011". If you want to match any one year, let us know.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.