What if the first line contains bar too? Then it prints twice with your version. awk solution:
awk 'NR == 1 { print } NR > 1 && $0 ~ "bar" { print }' FILE
If you want the search to look like almost the last element in the string:
awk 'ARGIND > 1 { exit } NR == 1 { print } NR > 1 && $0 ~ ARGV[2] { print }' FILE YOURSEARCHSTRING 2>/dev/null
sed Solution:
sed -n '1p;1d;/bar/p' FILE
The advantage for both of them is that it is one process.
source share