Solved

Linux Shell Scripting: Trim last slash

Posted on 2013-11-08
4
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39634567
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
ID: 39634575
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
ID: 39634608
"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
ID: 39635082
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

695 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