3

Let's say I have a file, foo.txt with certain contents:

abcds9912:        8c b2           1e12
asddb221          jj 7u 9i         e2e1

Now I want to grep only 8c, b2, jj, 7u, 9i i.e characters coming in pairs

So far I have tried

cat foo.txt | grep -Eo '[a-z0-9]{2}'

but it didn't work

Paolo
  • 21,270
  • 6
  • 38
  • 69

4 Answers4

5

You need to use word boundaries, you can use:

\b[a-z0-9]{2}\b

Your command is:

cat foo.txt | grep -Eo '\b[a-z0-9]{2}\b'
Paolo
  • 21,270
  • 6
  • 38
  • 69
0

Following may also help you here.

awk --re-interval -v RS=" " -v FS="" '/^[[:alnum:]]{2}$/' Input_file

Since my awk version is OLD I am using --re-interval you could remove it in case you are using new version of wk.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0
 cat foo.txt | grep -E -w -o '\w{2}'
0

You can use word boundary with character classes to achieve this

$ cat foo.txt | grep -Eo '\b[[:alnum:]]{2}\b'

or

$ grep -Eo '\b[[:alnum:]]{2}\b' foo.txt

Paolo
  • 21,270
  • 6
  • 38
  • 69
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41