?
Solved

Linux Shell Scripting: Trim last slash

Posted on 2013-11-08
4
Medium Priority
?
390 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 800 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 1200 total points
ID: 39635082
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

800 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