Remove all the characters from string after last '/'

Terence

I have the followiing input file and I need to remove all the characters from the strings that appear after the last '/'. I'll also show my expected output below.

input:

/start/one/two/stopone.js
/start/one/two/three/stoptwo.js
/start/one/stopxyz.js

expected output:

/start/one/two/
/start/one/two/three/
/start/one/

I have tried to use sed but with no luck so far.

hek2mgl

You could simply use good old grep:

grep -o '.*/' file.txt

This simple expression takes advantage of the fact that grep is matching greedy. Meaning it will consume as much characters as possible, including /, until the last / in path.


Original Answer:

You can use dirname:

while read line ; do
    echo dirname "$line"
done < file.txt

or sed:

sed 's~\(.*/\).*~\1~' file.txt

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove all the characters from string after last '/'

From Dev

Remove all characters except last n characters from a string

From Dev

Remove all characters except last n characters from a string

From Dev

Remove all characters after the last letter of the string JS

From Dev

Remove all characters from a string from last comma onwards

From Dev

PHP Regex — Remove all characters but first from last word in string

From Dev

Remove last characters from string

From Dev

Get all characters after the last '/' (slash) from url string

From Dev

Remove all characters from a string after coma if comma exists

From Dev

Remove characters after the last appearance of a delimiter in a string

From Dev

Remove all characters from the string

From Dev

Remove the last X specific characters from string

From Dev

Bash remove first and last characters from a string

From Dev

Remove Punctuation characters at last from a string

From Dev

Bash remove first and last characters from a string

From Dev

Remove last two characters from a string if it is -C

From Dev

Notepad ++ How to remove all characters after a string

From Dev

powershell trim - remove all characters after a string

From Dev

Remove all characters of a string after the fourth space

From Dev

Extract characters from string after last period

From Dev

Remove the last characters of a string

From Dev

TSQL how to remove all characters or text after the last tab space

From Dev

NSString - remove all characters after last white space

From Dev

TSQL how to remove all characters or text after the last tab space

From Dev

Remove all special characters from string

From Dev

Remove all hex characters from string in Python

From Dev

How to remove all '\' characters from a string in java

From Dev

Remove all space characters from string in Rails

From Dev

Autohotkey: Remove ALL single characters from a string

Related Related

  1. 1

    Remove all the characters from string after last '/'

  2. 2

    Remove all characters except last n characters from a string

  3. 3

    Remove all characters except last n characters from a string

  4. 4

    Remove all characters after the last letter of the string JS

  5. 5

    Remove all characters from a string from last comma onwards

  6. 6

    PHP Regex — Remove all characters but first from last word in string

  7. 7

    Remove last characters from string

  8. 8

    Get all characters after the last '/' (slash) from url string

  9. 9

    Remove all characters from a string after coma if comma exists

  10. 10

    Remove characters after the last appearance of a delimiter in a string

  11. 11

    Remove all characters from the string

  12. 12

    Remove the last X specific characters from string

  13. 13

    Bash remove first and last characters from a string

  14. 14

    Remove Punctuation characters at last from a string

  15. 15

    Bash remove first and last characters from a string

  16. 16

    Remove last two characters from a string if it is -C

  17. 17

    Notepad ++ How to remove all characters after a string

  18. 18

    powershell trim - remove all characters after a string

  19. 19

    Remove all characters of a string after the fourth space

  20. 20

    Extract characters from string after last period

  21. 21

    Remove the last characters of a string

  22. 22

    TSQL how to remove all characters or text after the last tab space

  23. 23

    NSString - remove all characters after last white space

  24. 24

    TSQL how to remove all characters or text after the last tab space

  25. 25

    Remove all special characters from string

  26. 26

    Remove all hex characters from string in Python

  27. 27

    How to remove all '\' characters from a string in java

  28. 28

    Remove all space characters from string in Rails

  29. 29

    Autohotkey: Remove ALL single characters from a string

HotTag

Archive