Anuj
Anuj

Reputation: 1200

Using sed to extracting data from file

I have a file with lines like following:

{u'phone_num': u'9999999999', u'name': u'abc', u'format': u'json'}

I am trying to extract the phone number i.e. 9999999999 from each line.

The sed I am using is not working.

echo "{u'phone_num': u'9999999999', u'name': u'abc', u'format': u'json'}" | sed 's/.*phone_num.*\([[:digit:]]\+).*/\1/'

This is printing the whole line and not just the digits.

Upvotes: 1

Views: 156

Answers (2)

damgad
damgad

Reputation: 1446

Yet another solution using awk

echo "{u'phone_num': u'9999999999', u'name': u'abc', u'format': u'json'}" | awk -F "'" '{ print $4 }'

Upvotes: 1

anubhava
anubhava

Reputation: 784888

You can use this sed:

sed "s/^{u'phone_num':[[:blank:]]*u'\([^']*\).*$/\1/" file
9999999999

Upvotes: 1

Related Questions