All Articles

The ABC's of p4 grep - A

Welcome to The ABC’s of p4 grep!

Here we will explore the different flags for the p4 grep commands.

Let’s start!

A

Much like the regular grep, use the -A flag to show the lines after the matching search term is found.

Suppose you have a file named test.txt with the following content:

sad
happy
awake
coffee
work
school

You would search for the word “happy” like this:

$ grep -e happy test.txt
//depot/test.txt#6:happy

To see the first 2 lines after happy, execute the following:

$ grep -A 2 -e happy test.txt
//depot/test.txt#6:happy
//depot/test.txt#6-awake
//depot/test.txt#6-coffee

Now the lowercase -a flag stands for “all revisions”. p4 grep by default only greps through the latest revision.

Suppose we have a helloworld.txt file with the following content:

p4 print helloworld.txt
//depot/helloworld.txt#2 - edit change 9 (text)
Hello world!

Let’s search for the term “earth”:

p4 grep -e earth helloworld.txt

Nothing. Well, duh! Didn’t you see above that the content of the file is Hello world! This is true for the latest revision (#2), but we can p4 print the first revision to reveal:

p4 print helloworld.txt#1
//depot/helloworld.txt#1 - add change 8 (text)
Hello earth!

So the word “earth” was in the file, this is where the -a flag comes in handy.

p4 grep -a -e earth helloworld.txt
//depot/helloworld.txt#1:Hello earth!

So use -a when searching for a term in all the revisions of a file.

Published May 27, 2025

I love coffee, coding and writing.