Jepper
Jepper

Reputation: 1111

regex match specific pattern

I have

[root@centos64 ~]# cat /tmp/out
[
  "i-b7a82af5",
  "i-9d78f4df",
  "i-92ea58d0",
  "i-fa4acab8"
]

I would like to pipe though sed or grep to match the format "x-xxxxxxxx" i.e. a mix of a-z 0-9 always in 1-[8 chars length], and omit everything else

[root@centos64 ~]# cat /tmp/out| sed s/x-xxxxxxxx/
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8

I know this is basic, but I can only find examples of text substitution.

Upvotes: 1

Views: 90

Answers (5)

Ed Morton
Ed Morton

Reputation: 204731

Why not just print whatever's between the quotes:

$ sed -n 's/[^"]*"\([^"]*\).*/\1/p' file
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8

$ awk -F\" 'NF>1{print $2}' file                     
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8

Upvotes: 1

batman
batman

Reputation: 269

This should work ^[a-z0-9]-[a-zA-Z0-9]{8}$

Upvotes: 0

Barmar
Barmar

Reputation: 782785

grep -Eo '[a-z0-9]-[a-z0-9]{8}' file

The -E option makes it recognize extended regular expressions, so it can use {8} to match 8 repetitions.

The -o option makes it only print the part of the line that matches the regexp.

Upvotes: 1

dshepherd
dshepherd

Reputation: 5457

I think this is all you need: [0-9a-zA-Z]-[0-9a-zA-Z]{8}. Try it out here.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174874

Through GNU sed,

$ sed -nr 's/.*([a-z0-9]-[a-z0-9]{8}).*/\1/p' file
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8

Upvotes: 0

Related Questions