-1

I am verifying UID/GID for certain users using the dscacheutil command. Using grep with -A flag gives me what I need but the number of lines doesn't make sense. Output below:

Edit8:~ edit08$ dscacheutil -q user | grep -A 6 "userX"
name: userX
password: ********
uid: 2063
gid: 1021
dir: /Users/userX
shell: /bin/sh
gecos: User X

name: userA
password: ********
uid: 2013
Edit8:~ edit08$ dscacheutil -q user | grep -A 4 "userX"
name: userX
password: ********
uid: 2063
gid: 1021
dir: /Users/userX
shell: /bin/sh
gecos: User X

name: userA
Edit8:~ edit08$ dscacheutil -q user | grep -A 2 "userX"
name: userX
password: ********
uid: 2063
--
dir: /Users/userX
shell: /bin/sh
gecos: User X
Edit8:~ edit08$ dscacheutil -q user | grep -6 "userX"
uid: 2012
gid: 1010
dir: /Users/userB
shell: /bin/sh
gecos: User B

name: userX
password: ********
uid: 2063
gid: 1021
dir: /Users/userX
shell: /bin/sh
gecos: User X

name: userA
password: ********
uid: 2013
Edit8:~ edit08$ dscacheutil -q user | grep -A 2 "userX"
name: userX
password: ********
uid: 2063
--
dir: /Users/userX
shell: /bin/sh
gecos: User X
Edit8:~ edit08$ dscacheutil -q user | grep -A 3 "userX"
name: userX
password: ********
uid: 2063
gid: 1021
dir: /Users/userX
shell: /bin/sh
gecos: User X

Edit8:~ edit08$ 

While my logic dictates the magic number of lines would be 6, in fact it is 3.

Anybody?

Thanks

1
  • Is it only outputting 3 because that's all there is in the input? Mar 9, 2012 at 19:33

2 Answers 2

1

I've marked with ~~~ the lines that match the pattern, and numbered the lines thereafter:

    Edit8:~ edit08$ dscacheutil -q user | grep -A 6 "userX"
~~~ name: userX
 1  password: ********
 2  uid: 2063
 3  gid: 1021
~~~ dir: /Users/userX
 1  shell: /bin/sh
 2  gecos: User X
 3  
 4  name: userA
 5  password: ********
 6  uid: 2013


    Edit8:~ edit08$ dscacheutil -q user | grep -A 3 "userX"
~~~ name: userX
 1  password: ********
 2  uid: 2063
 3  gid: 1021
~~~ dir: /Users/userX
 1  shell: /bin/sh
 2  gecos: User X
 3

As you can see, grep -A n never shows you a line that's more than n after the relevant match; it's just that you have some matches close together, which result in long stretches of included lines.

2
  • Yes that makes sense, thank you. So if the string$ is found before the end of # requested lines, it starts over...? Unfortunately this is the output for dscacheutil so I just have to live with it
    – user1183817
    Mar 9, 2012 at 23:31
  • 1
    @user1183817: Well, you can grep for " userX" (with a space before the username) instead of just "userX". Or, heck, you could grep for the entire line, "^name: userX$". Either of these would only match the name line, not the dir line, so the -A 6 would only give six lines after the name line.
    – ruakh
    Mar 9, 2012 at 23:42
0

From the man page:

-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

In all the examples you give, each instance of userX is followed by the correct number of lines of context. In cases where another instance of userX occurs within the context of another, the context extends to however many lines after the second instance.

2
  • grep -6 "userX" is equivalent to grep -C 6 userX (-C for "context"), and therefore to grep -A 6 -B 6 userX (-A for "after", -B for "before").
    – ruakh
    Mar 9, 2012 at 22:53
  • grep -6 displays 6 lines in both directions from string
    – user1183817
    Mar 9, 2012 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy