Bootstrap FreeKB - Bash (Scripting) - do something forward slashes
Bash (Scripting) - do something forward slashes

Updated:   |  Bash (Scripting) articles

Let's say you have a string that contains forward slash, like this.

http://www.example.com/foo/bar

 

awk can be used to cut the string apart. $1 will print the protocol (http in this example).

~]# echo "http://www.example.com/foo/bar" | awk -F'/' '{print $1}'
http:

 

$3 will print the hostname (www.example.com).

~]# echo "http://www.example.com/foo/bar" | awk -F'/' '{print $3}'
www.example.com

 

$4 will print the first part of the context root (foo in this example).

~]# echo "http://www.example.com/foo/bar" | awk -F'/' '{print $4}'
foo

 

The following regular expression will remove everything before the first forward slash.

~]# echo "http://www.example.com/foo/bar" | sed 's|^[^/]*/||g'
/www.example.com/foo/bar

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 2bc09b in the box below so that we can be sure you are a human.