Solved

Linux Shell Scripting: Trim last slash

Posted on 2013-11-08
4
380 Views
Last Modified: 2013-11-10
In both of these cases, I want "http://example.com" to be returned.

If there is a slash on the end it should be trimmed.

u="http://example.com/xyz"
x=${u%/*}
echo $x
u="http://example.com/xyz/"
x=${u%/*}
echo $x

Open in new window

0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
u="http://example.com"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
u="http://example.com/xyz"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
u="http://example.com/xyz/"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
u="http://example.com/abc/def/xyz"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
u="http://example.com/abc/def/xyz/"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
In these examples I would want:
http://example.com/abc/def/xyz


I only want the trailing slash to be trimmed.


u="http://example.com/abc/def/xyz"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
u="http://example.com/abc/def/xyz/"
x=$(echo $u |awk -F"/|//" '{print $1 "//" $2}')
echo $x
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
Comment Utility
"I only want the trailing slash to be trimmed."

and

"In both of these cases, I want "http://example.com" to be returned."

Strange contradiction. Anyway:

u="http://example.com/"
x=$(echo $u | sed 's/\/$//')
echo $x
u="http://example.com"
x=$(echo $u | sed 's/\/$//')
echo $x
u="http://example.com/xyz"
x=$(echo $u | sed 's/\/$//')
echo $x
u="http://example.com/xyz/"
x=$(echo $u | sed 's/\/$//')
echo $x
u="http://example.com/abc/def/xyz/"
x=$(echo $u | sed 's/\/$//')
echo $x
u="http://example.com/abc/def/xyz"
x=$(echo $u | sed 's/\/$//')
echo $x

To  clarify:

s/\/$//

is

"s" "slash" "backslash" "slash" "dollar" "slash" "slash"
0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
Comment Utility
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now