Regex a field from string; sed and Linux

How can I use sed to extract the value of the serializedlength, 3600605 in this case, from the following string?

Value at:0x7f36dda576f0 refcount:1 encoding:skiplist serializedlength:3600605 lru:1837863 lru_seconds_idle:20

If not sed, when how?

Who is Participating?

I do not believe sed is what you are looking for as this is commonly used to modify strings, I would alternatively recommend awk or cut.

I placed your string into a file to provide an example on how I would extract the required data if being piped from another program.

My first examples pull serializedlength and the value out.

cat /tmp/eetmp | cut -f 5 -d " "

cat /tmp/eetmp | awk -F " " '{ print $5 }'

awk -F " " '{ print $5 }' < /tmp/eetmp

My second examples pull just the value of serializedlength.

cat /tmp/eetmp | awk -F " " '{ print $5 }' | awk -F ":" '{ print $2 }'

cat /tmp/eetmp | cut -d " " -f 5 | cut -d ":" -f 2

awk -F " " '{ print $5 }' < /tmp/eetmp | cut -d ":" -f 2

as you can see there are many different variants on how to do this, if you require any further help please let me know. - the 5 and 2 indicate which delimited values I require.

I hope the above helps.

Kind Regards,
käµfm³d 👽Commented:
How about grep?


echo "Value at:0x7f36dda576f0 refcount:1 encoding:skiplist serializedlength:3600605 lru:1837863 lru_seconds_idle:20" | grep -iPo '(?<=serializedlength:)\d+'

Open in new window

Tested on GNU grep 2.14.
brothertomAuthor Commented:
Thank you very much - the double cut works nicely for me :)

brothertomAuthor Commented:
kaufmed - thanks for the grep example, however there appears to be no -P option in my grep version - looks interesting though.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.