zer0Id0l
zer0Id0l

Reputation: 1444

Get value of parameter from text file using sed

I am trying to extract the value of a parameter from the text file.

Below is my text file with uri_param as parameter.

application.txt
---------------
uri_param=frontier://tenant=stripe;env=qa;[email protected]:80

command:
--------
egrep ^uri_param application.txt | sed -e 's/.*=//'

I am expecting the strign after first = as output i.e. frontier://tenant=stripe;env=qa;[email protected]:80, but the output I am getting is [email protected]:80.

How can I fix this? What I found till now is .* in sed is greedy and it will match the longest string after =.

Upvotes: 1

Views: 3698

Answers (4)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -n '/^uri_param=/ {s///p;q;}' YourFile
  • extract only the first occurance of the uri_param, remove this uri_param= (replace by nothing) and print it then quit.
  • OK for small and medium file (a grep is faster enough on a big file like 100 Mb)

Upvotes: 3

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

We can filter it in the grep part:

grep -o "^uri_param=.*:[0-9]\{1,\}" infile|sed -e "s/^uri_param=//"

Or use a more flexible tool like gawk:

gawk 'match($0, "^uri_param=(.*:[0-9]+)", r){print r[1]}' infile

NOTE: If your url doesn't finish allways with the port number the pattern should be adjusted.

Upvotes: 1

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

sed -r 's/^[_0-9a-zA-Z]+=//g' File_Name

Upvotes: 1

Kalanidhi
Kalanidhi

Reputation: 5092

You can try this way

sed -r s'/[^=]+.(.*)/\1/g' File_Name

Upvotes: 1

Related Questions