How to Use grep to Extract URL from File


Often you may want to use the grep command in Bash to extract a URL from a file.

You can use the following syntax to do so:

grep -Eo 'https://.*' player_info.txt

This particular example will extract all URL’s that start with the pattern https:// in the file named player_info.txt.

Note that you could also extract all URL’s that start with the pattern https:// or http:// by using the following syntax:

grep -Eo '(http|https)://.*' player_info.txt

Here is what the various commands do:

  • E: Specifies that the pattern that follows should be interpreted as an extended regular expression
  • o: Specifies that only the matching text pattern should be printed

The following example shows how to use this syntax in practice.

Example: How to Use grep to Extract URL from File

Suppose that we have a filed named player_info.txt that contains information about various basketball players.

We can use the cat command to view the contents of this file:

Suppose that we would like to extract any URL from the file that starts with the pattern https://.

We can use the following syntax to do so:

grep -Eo 'https://.*' player_info.txt

The following screenshot shows how to use this syntax in practice:

grep extract URL from file

Notice that this returns the following URL from the file:

  • https://www.nba.com/stats/players

Note that we could also use the following syntax to extract any URL that starts with either https:// or http:// in the file:

grep -Eo '(http|https)://.*' player_info.txt

The following screenshot shows how to use this syntax in practice:

grep extract URL with multiple patterns from file

Notice that this returns the following URL’s from the file:

  • https://www.nba.com/stats/players
  • http://www.nba.com/stats/leaders

Feel free to use whichever method you prefer depending on the URL pattern that you would like to match.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

How to Use grep to List All Files Containing Specific String
How to Use grep to Get Line Number of Match
Bash: How to Filter for Lines in File that Start with Specific String

Leave a Reply