Remove all leading and trailing slashes on each line of a text file

sed -e "s,/\+$,," -e "s,^/\+,," file.txt
There can be more than one trailing slash, all of them will be removed.

-1
By: bugmenot
2012-11-02 21:08:30
sed

These Might Interest You

  • Remove empty lines additionally: tr -s ' \t\n' <1.txt >2.txt identical with: tr -s '[:space:]' <1.txt >2.txt To "clean perfectly" a text or code file, You can combine this command with another one: while read l; do echo -e "$l"; done <1.txt >2.txt (= remove all leading and trailing spaces or tabs from all lines of a text file)


    2
    tr -s ' \t' <1.txt >2.txt
    knoppix5 · 2012-12-19 07:36:00 0
  • Bash only, no sed, no awk. Multiple spaces/tabs if exists INSIDE the line will be preserved. Empty lines stay intact, except they will be cleaned from spaces and tabs if any available.


    5
    while read l; do echo -e "$l"; done <1.txt >2.txt
    knoppix5 · 2012-03-13 14:27:49 0
  • the advantage to doing it this way is that you can adjust the max depth to get more recursive results and run it on non GNU systems. It also won't print trailing slashes, which can easily be removed, but can be slightly annoying.. You could run: # for file in `find * -maxdepth 0 -type d`;do ls -d $file;done and in the ls -d part of the command you can put in whatever parameters you want to get things like permissions, time stamps, and ownership. Show Sample Output


    0
    find * -maxdepth 0 -type d
    sonic · 2013-02-25 21:10:49 1
  • Clone a root partition. The reason for double-mounting the root device is to avoid any filesystem overlay issues. This is particularly important for /dev. Also, note the importance of the trailing slashes on the paths when using rsync (search the man page for "slash" for more details). rsync and bash add several subtle nuances to path handling; using trailing slashes will effectively mean "clone this directory", even when run multiple times. For example: run once to get an initial copy, and then run again in single user mode just before rebooting into the new disk. Using file globs (which miss dot-files) or leaving off the trailing slash with rsync (which will create /mnt/target/root) are traps that are easy to fall into.


    1
    mount /dev/root /mnt/root; rsync -avHX /mnt/root/ /mnt/target/
    jharr · 2011-08-24 14:29:17 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: