0

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.

Nick
  • 3
  • 1

4 Answers4

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://'
  1. Select all lines with the substring "EID:"
  2. Remove everything after "EID:" plus 6 characters
  3. Remove everything before (and including) "EID:"
  • 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