Solved

Linux Shell Scripting: Trim last slash

Posted on 2013-11-08
4
387 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

734 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