I have a file called in.txt which contains a whole bunch of code, however I need to extract a user ID which is guaranteed to be of the form 'EID:nmb685', potentially with content before and/or after the guaranteed format. I want to extract the 'nmb685' using a bash script. I've tried some combinations of grep and sed but nothing has worked.
Asked
Active
Viewed 523 times
0
-
1I see "unix" tag, pls report your grep version. – Kent Apr 24 '15 at 08:16
4 Answers
0
If your grep supports -P
, perl-regexp parameter, you may use this.
grep -oP 'EID:\K\w+' file

Avinash Raj
- 172,303
- 28
- 230
- 274
0
What is being output after the ID? Is there anything consistent that you can match against?
If you know the length of the userid you can use:
grep "EID:......" in.txt > out.txt
or if you don't maybe something like this (checks all char/num followed by space, preceeded by EID:)
grep "EID:[A-Za-z0-9]* " in.txt > out.txt

Hunter Frazier
- 487
- 5
- 9
0
Not very elegant, but this works:
grep "EID:" in.txt | sed 's/\(.*\EID:......\).*/\1/g' | sed 's/^.*EID://'
- Select all lines with the substring "EID:"
- Remove everything after "EID:" plus 6 characters
- Remove everything before (and including) "EID:"

Mick Watkins
- 34
- 5
-
this may have problem if one line containing multiple patterns like `EID:foo EID:bar`. but OP didn't mention that, so it might be ok as well. – Kent Apr 24 '15 at 08:21
0
if your grep doesn't support -p
but supports -o
, you can combine grep and awk.
grep -o 'EID:\w\+' file|awk -F':' '{print $2}'
Though can it be done by awk alone, but this is more straightforward.

Kent
- 189,393
- 32
- 233
- 301