Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linux Shell Scripting: Trim last slash

Posted on 2013-11-08
4
Medium Priority
?
394 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

618 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