Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read query parameter value from URL using perl command

Tags:

bash

url

perl

In shell, I need to extract specific query parameter from URI.

I tried to play around with this to get "offset" value

echo "/mypath/index.php?offset=20&query=uro" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->query_form("offset")'

But it always returns just offset=20&query=uro

Please help

like image 370
glaz666 Avatar asked Mar 01 '12 09:03

glaz666


People also ask

Can I pass URL as query parameter?

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string. but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

What is URL query parameter?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed.

How do I know if a URL has a parameter query?

To check if a url has query parameters, call the indexOf() method on the url, passing it a question mark, and check if the result is not equal to -1 , e.g. url. indexOf('? ') !== -1 .

What is a URL parameter?

URL parameters or query string parameters are used to send a piece of data from client to server via a URL. They can contain information such as search queries, link referrals, user preferences, etc..

How to read query parameters in JavaScript?

In this tutorial, learn different ways to read query parameters in javascript. query parameters are key and value pairs added to url to pass the simple parameters For example, if url is cloudhadoop.com/about?name=johh&dept=sales. window.location.search //name=johh&dept=sales location.search // name=johh&dept=sales

How do I get all the values associated with a parameter?

The URLSearchParams.getAll () method returns all of the values associated with a certain parameter: The URLSearchParams interface specifies the utility methods to work with the query string of a URL. The URLSearchParams suggests a consistent interface to the pieces of the URL and allows a manipulation of the query string (what comes after "?").

How to extract a parameter from a URL?

If the parameter is defined within URL then splitting it by &. var split_url = url.split ('?'); para_str = split_url [1]; if (para_str != undefined) { var parts = para_str.split ('&'); } After that, the process of parameter extraction is similar to the undefined case.


1 Answers

query_form returns a hash, change your script to:

perl -MURI -le 'chomp($url = <>); print +{URI->new($url)->query_form}->{offset}'

In order to process multiple lines:

perl -MURI -nle 'print +{URI->new($_)->query_form}->{offset}'
like image 112
Toto Avatar answered Oct 22 '22 08:10

Toto