Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

migrated from stackoverflow.com Mar 10 '12 at 14:45

This question came from our site for professional and enthusiast programmers.

    
Is it only outputting 3 because that's all there is in the input? –  Lars Kotthoff Mar 9 '12 at 19:33
add comment

2 Answers

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.

share|improve this answer
    
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 '12 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 '12 at 23:42
add comment

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.

share|improve this answer
    
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 '12 at 22:53
    
grep -6 displays 6 lines in both directions from string –  user1183817 Mar 9 '12 at 23:28
add comment

Your Answer

 
discard

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