1

How can I get all "clid's" which has the "client_type=0"? The pipe char "|" seperates each client info. The text, which I have is just one line:

clid=2 cid=3346 client_database_id=5153 client_nickname=xLukas\s\p\sLukas client_type=0|clid=3 cid=3346 client_database_id=1 client_nickname=powered\sby\sMirWayne.DE client_type=1|clid=4 cid=3346 client_database_id=9661 client_nickname=Number\sone client_type=0|clid=5 cid=3469 client_database_id=1 client_nickname=powered\sby\sMirWayne client_type=1|clid=6 cid=1716 client_database_id=1 client_nickname=FG\sBot client_type=1|clid=7 cid=3469 client_database_id=9661 client_nickname=Number\stwo client_type=0|clid=8 cid=1762 client_database_id=4351 client_nickname=pr0zkillz1x\pTobi client_type=0|clid=9 cid=1764 client_database_id=5160 client_nickname=xL1mited\s/\sMax client_type=0|clid=10 cid=1716 client_database_id=9704 client_nickname=Sebi client_type=0|clid=13 cid=3346 client_database_id=4336 client_nickname=checker284\s\p\sSebbo client_type=0|clid=19 cid=1762 client_database_id=9312 client_nickname=pr0skillz1x\pDanie client_type=0|clid=30 cid=3346 client_database_id=9697 client_nickname=TeamSpeakUser client_type=0|clid=33 cid=1716 client_database_id=1 client_nickname=serveradmin client_type=1

I need alle clid's, which has the client_type=0 in an extra line. For example like that:

    clid=2
    clid=3
    clid=4
    clid=5
    ...

My current solution is not working:

    cat clientlist.txt | grep -Eo "clid=[0-9]+[\d\D]*client_type=0" | grep -Eo "clid=[0-9]+"

Can somebody help?

3
  • 1
    cat a file into grep? Surely you jest! grep ... clientlist.txt | ... saves about a billion CPU cycles per invocation. Commented Nov 28, 2013 at 8:13
  • What's better? I would use each working solution, but currently I have no. Commented Nov 28, 2013 at 8:15
  • It's better to avoid a useless cat and a useless pipe. You don't drive with the brakes engaged, or do you? Commented Nov 28, 2013 at 9:03

2 Answers 2

1

The pipe char "|" seperates each client info.

Making use of this information, you can say:

tr '|' '\n' < clientlist.txt | grep -Po 'clid=\d+(?=.*client_type=0)'

For your input, it'd result in:

clid=2
clid=4
clid=7
clid=8
clid=9
clid=10
clid=13
clid=19
clid=30
Sign up to request clarification or add additional context in comments.

Comments

1

Using sed:

tr '|' '\n' < file | sed -n '/client_type=0/s/\(clid=[0-9]*\).*$/\1/p'
clid=2
clid=4
clid=7
clid=8
clid=9
clid=10
clid=13
clid=19
clid=30

10 Comments

close, but not quite - you need to handle the delimiter, so the correct version is sed -n -e 's/|/\n/' -e '/client_type=0/s/\(clid=[0-9]*\).*$/\1/p'
@OliverMatthews: Thanks I updated, I thought there are new lines after pipe in input file.
I started making use of grep more often after looking at your posts, you seem to have abandoned it!
np. Out of curiousity, why tr rather than extra parameters to sed?
@devnull: lol :) not intentionally. Somehow I thought sed is better for this but your grep is pretty kool one too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.