• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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?

  • 2
1 Solution

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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now