zjhui
zjhui

Reputation: 809

How to use sed to print the match patten

i want to use sed to print the match patten, for example:
i want to get the uuid , so i can use this :
blkid $1 | grep -o -E "[a-f0-9-]{8}([a-f0-9-]{4}){3}[a-f0-9-]{12}"
How can i do this use sed or awk ?

Upvotes: 3

Views: 3924

Answers (6)

mwfearnley
mwfearnley

Reputation: 3669

There's a nice list of sed one-liners at http://sed.sourceforge.net/sed1line.txt. It suggests two methods of emulating grep:

sed -n '/regexp/p'           # method 1
sed '/regexp/!d'             # method 2

/regexp/p prints the lines matching /regexp/. (The -n flag is needed to prevent sed from printing/reprinting every processed line.)

/regexp/!d deletes every line that doesn't match /regexp/, so only the matching lines are printed.

Upvotes: 0

Vijay
Vijay

Reputation: 67291

use sed:

sed -n 's/.*\(Pattern\).*/\1/p'

tested

echo "< TextValue > Start < /TextValue >" < TextValue > Start < /TextValue > echo "< TextValue > Start < /TextValue >" | sed -n 's/.(Start)./\1/p' Start

Upvotes: 0

Luke B
Luke B

Reputation: 2461

blkid | sed -nr 's/^.+UUID="(.*?)" .+$/\1/p'

Upvotes: 1

procleaf
procleaf

Reputation: 738

sed

sed -n 's/pattern/&/p' file

-n is to tell sed to be quiet, & is matched string, p is to print.

awk

awk '/pattern/' file

in your case, change pattern to [a-f0-9-]{8}([a-f0-9-]{4}){3}[a-f0-9-]{12}, may need to use \ to escape [.

Upvotes: 4

gvalkov
gvalkov

Reputation: 4107

Awk and sed solutions:

$ blkid /dev/sda2 | sed -e 's/.*UUID="\([0-9A-F]*\).*/\1/'
16A42BA2A42B837B

$ blkid /dev/sda2 | awk '{split($2, tmp, "=") ; print tmp[2]}'
"16A42BA2A42B837B"

$ blkid /dev/sda2 | awk -F'UUID="|"' '{print $2}'
16A42BA2A42B837B

Upvotes: 0

Kent
Kent

Reputation: 195229

you want this?

awk:

kent$  echo '/dev/sda6: UUID="c6e3ce88-f44e-4261-9178-042db8423081" TYPE="ext3"'|awk -F'UUID="|" ' '{print $2}'
c6e3ce88-f44e-4261-9178-042db8423081

sed:

kent$  echo '/dev/sda6: UUID="c6e3ce88-f44e-4261-9178-042db8423081" TYPE="ext3"'|sed -r 's/.*UUID="([^"]*).*"/\1/g'
c6e3ce88-f44e-4261-9178-042db8423081

Upvotes: 3

Related Questions