Assuming a simple grep such as:$ psa aux | grep someApp1000     11634 51.2  0.1  32824  9112 pts/1    SN+  13:24   7:49 someAppThis provides much information, but as the first line of the ps command is missing there is no context for the info. I would prefer that the first line of ps be shown as well:$ psa aux | someMagic someAppUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND1000     11634 51.2  0.1  32824  9112 pts/1    SN+  13:24   7:49 someAppOf course, I could add a regex to grep specifically for ps:$ ps aux | grep -E COMMAND|someAppHowever, I would prefer a more general solution as there are other cases in which I would like to have the first line as well.
How to grep a specific line _and_ the first line of a file?
bash;command line
Good wayNormally you can't do this with grep but you can use other tools. AWK was already mentioned but you can also use sed, like this:sed -e '1p' -e '/youpattern/!d'How it works:Sed utility works on each line individually, running specified commands on each of them. You can have multiple commands, specifying several -e options. We can prepend each command with a range parameter that specifies if this command should be applied to specific line or not.1p is a first command. It uses p command which normally prints all the lines. But we prepend it with a numerical value that specifies the range it should be applied to. Here, we use 1 which means first line. If you want to print more lines, you can use x,yp where x is first line to print, y is last line to print. For example to print first 3 lines, you would use 1,3pNext command is d which normally deletes all the lines from buffer. Before this command we put yourpattern between two / characters. This is the other way (first was to specify which lines as we did with p command) of addressing lines that the command should be running at. This means the command will only work for the lines that match yourpattern. Except, we use ! character before d command which inverts its logic. So now it will remove all the lines that do not match specified pattern.At the end, sed will print all the lines that are left in buffer. But we removed lines that do not match from the buffer so only matching lines will be printed.To sum up: we print 1st line, then we delete all the lines that do not match our pattern from input. Rest of the lines are printed (so only lines that do match the pattern).First line problemAs mentioned in comments, there is a problem with this approach. If specified pattern matches also first line, it will be printed twice (once by p command and once because of a match). We can avoid this in two ways:Adding 1d command after 1p. As I already mentioned, d command deletes lines from buffer and we specify it's range by number 1, which means it will only delete 1st line. So the command would be sed -e '1p' -e '1d' -e '/youpattern/!d'Using 1b command, instead of 1p. It's a trick. b command allows us to jump to other command specified by a label (this way some commands can be omitted). But if this label is not specified (as in our example) it just jumps to the end of commands, ignoring rest of the commands for our line. So in our case, last d command won't remove this line from buffer.Full example:ps aux | sed -e '1b' -e '/syslog/!d'Using semicolonSome sed implementations can save you some typing by using semicolon to separate commands instead of using multiple -e options. So if you don't care about being portable the command would be ps aux | sed '1b;/syslog/!d'. It works at least in GNU sed and busybox implementations.Crazy wayHere's, however, rather crazy way to do this with grep. It's definitely not optimal, I'm posting this just for learning purposes, but you may use it for example, if you don't have any other tool in your system:ps aux | grep -n '.*' | grep -e '\(^1:\)\|syslog'How it worksFirst, we use -n option to add line numbers before each line. We want to numerate all the lines we we are matching .* - anything, even empty line. As suggested in comments, we can also match '^', result is the same.Then we are using extended regular expressions so we can use \| special character which works as OR. So we match if the line starts with 1: (first line) or contains our pattern (in this case its syslog).Line numbers problemNow the problem is, we are getting this ugly line numbers in our output. If this is a problem, we can remove them with cut, like this:ps aux | grep -n '.*' | grep -e '\(^1:\)\|syslog' | cut -d ':' -f2--d option specifies delimiter, -f specifies fields (or columns) we want to print. So we want to cut each lines on every : character and print only 2nd and all subsequent columns. This effectively removes first column with it's delimiter and this is exactly what we need.