Substitution to remove tailing slash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitution to remove tailing slash?
# 1  
Old 05-05-2015
Substitution to remove tailing slash?

Heyas

I am trying to remove a tailing space, with substitution.
Already tried some multiple escapes with no luck, is that even possible?
Code:
echo $CHROOT
[ -z "$CHROOT" ] || \
	CHROOT="${CHROOT/\/$}"
echo $CHROOT
return

And all i get is:
Code:
:) paths $ CHROOT=/usr/local/
+ paths $ . *
/usr/local/
/usr/local/

Code:
$SHELL -version
GNU bash, version 4.3.33(1)-release (x86_64-redhat-linux-gnu)

The 2nd printed output, would be expected to be:
Code:
/usr/local

Thank you

EDIT:
I can check if the last string is a slash or not, but i thought a subs would work just fine?
# 2  
Old 05-05-2015
Try:
Code:
CHROOT=${CHROOT%/}

This strips a trailing slash. Your attempt to use substitution would work, but the pattern isn't regex. You'd use ${CHROOT/%\//}

See relevant section of the bash manual:
Quote:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with ‘#’, it must match at the beginning of the expanded value of parameter. If pattern begins with ‘%’, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If parameter is ‘@’ or ‘*’, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 05-06-2015
neutronscott solution is the % (Remove matching suffix pattern) expansion not replace. It happens to product the same result in this case. The replace version should be:
Code:
${CHROOT/%\//}

I'll try and highlight the difference here:

Code:
$ CHROOT=/test/this/

$ echo ${CHROOT/%\//}
/test/this

$ echo ${CHROOT%/}
/test/this

$ echo ${CHROOT/%\//Z}
/test/thisZ

$ echo ${CHROOT%/Z}
/test/this/

In the 3rd example will replace the last slash with Z
In the 4th example we try and delete /Z from end of string (no match, so nothing deleted).

Last edited by Chubler_XL; 05-06-2015 at 12:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove leading dot and slash on relative paths with find

When I specify a directory by name the leading ./ is not shown: $ find somedir/ somedir/a.bin somedir/target/out.binBut when I specify current dir it adds the ./ to the beginning of each result: $ find . | grep somedir ./somedir/a.bin ./somedir/target/out.binIs there any particular reason why... (2 Replies)
Discussion started by: Tribe
2 Replies

2. Shell Programming and Scripting

awk logic for tailing and printing

if the last line of an output contains a certain string 'FAILED', i want to print 200 lines from the output. here's where I got stuck: process blah blah blah | awk '{if ($0 ~ /FAILED/) y=x "\n" $0; x=$0};END{print y}' the above only prints the last 2 lines, and it also searches the... (19 Replies)
Discussion started by: SkySmart
19 Replies

3. Shell Programming and Scripting

Better way to do tailing with awk

my current code: varA=$(tail -200 /var/log/data.txt | egrep -c "Capital|capitol") varB=$(tail -200 /var/log/data.txt | egrep -c "State|Country") varC=$(tail -200 /var/log/data.txt | egrep -c "City|Town") I want to do this a different way. something like: AllVars=$(echo $(tail -200... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Tailing and counting lines

tail -f /var/log/syslog | egrep -c FATAL is there a way to do the above and actually have the number of lines matching the pattern increment as it is logged to the log file? for instance, when you invoke a command like the one i just posted, you'll not get the total lines unless you do... (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

Need to remove slash

I got stuck in problem i need to remove slash but it is not working. current output /usr/sbin/httpd expected output usr sbin httpd (4 Replies)
Discussion started by: learnbash
4 Replies

6. UNIX for Dummies Questions & Answers

tailing a file which contains Control chracters

Hi. I have a log file which gets updated by a java process and it uses ASCII STX and ETX characters (i.e CTRL-B and CTRL-C characters) to demarcate each XML message logged. so the format of the file is something like STX XML_MESSAGE1 .. .. ETX STX XML_MESSAGE2 .. .. ETX each XML... (4 Replies)
Discussion started by: gregoryp
4 Replies

7. Shell Programming and Scripting

Tailing last modified part of log file

I have a log file which contains data like this This log file is updated twice a day at 7am and 6pm, I want a script(which i will make run at 7:10am and 6:10pm) which should fetch only the last appended lines since last update.. I mean.. if i execute the script at 7.10am 3/3/2010 it... (4 Replies)
Discussion started by: user__user3110
4 Replies

8. Shell Programming and Scripting

Using sed to append backward slash before forward slash

Hi all, I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front... (2 Replies)
Discussion started by: sarbjit
2 Replies

9. UNIX for Advanced & Expert Users

Substitute single backward-slash with the double backward-slash

Hi, I have a path like this c:\test\sample\programs, i need to change thiis to c:\\test\\sample\\programs. How to perform this? I tried tr command but it didn't help me. Thanks Vijayan (3 Replies)
Discussion started by: mvictorvijayan
3 Replies

10. UNIX for Dummies Questions & Answers

tailing logs

Hi I'd like to achieve the ff functionality; tail -f log | grep keyword ...... and then perform a function. That is, I like to tail a log and when a certain keyword appears I then want my script to play an audio file for example. Any ideas?? Cheers M (1 Reply)
Discussion started by: squeakywheel
1 Replies
Login or Register to Ask a Question