How do I remove the last characters from a string?

Rak kundra

I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2.

I tried with the following script, but it doesn't work as expected.

var=type_cardio_10-11-2017
var2=${var} | cut -f1 -d "_"
echo ${var2}

The output I want is type_cardio.

Kusalananda

To remove everything from after the last _ in var and assign the result to var2:

var2=${var%_*}

The parameter expansion ${parameter%word} removes the pattern word (_* in this case) from the end of the value of the given variable.

The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.

To remove the last underscore and the 10 characters after it, use

var2=${var%_??????????}

To remove characters corresponding to a date string such as the one in your example, use

var2=${var%_[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]}

Which pattern you use depends on how carefully you want to perform the match.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I remove multiple offending characters from my string?

From Dev

How do I remove new line characters from a string?

From Dev

How do I remove the first n characters from a string in c?

From Dev

Remove last characters from string

From Dev

How do I remove the last line of a string that has visible characters in ruby?

From Dev

How do I remove multiple characters in a string

From Dev

How do I remove unknown characters in a string?

From Dev

How do I remove unknown characters in a string?

From Dev

How do you remove the first and the last characters from a Multimap's String representation?

From Dev

How do I remove the first or last n characters from a value in q/ kdbstudio?

From Java

How to remove last n characters from a string in Bash?

From Dev

how to remove first and last characters from exploded php string

From Dev

How to remove last 3 characters from a list of String in java?

From Dev

Remove the last X specific characters from string

From Dev

Bash remove first and last characters from a string

From Dev

Remove all the characters from string after last '/'

From Dev

Remove Punctuation characters at last from a string

From Dev

Remove all the characters from string after last '/'

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

In an expect script, how do I remove a set of special characters from a string variable?

From Java

How do I remove all non alphanumeric characters from a string except dash?

From Dev

How do I remove copyright and other non-ASCII characters from my Java string?

From Dev

Remove the last characters of a string

From Dev

How do I remove all but the last occurrence of a string?

From Dev

how do i remove the first characters of a string [python]

From Dev

How do I remove CSS from a string?

From Dev

How Do I Remove A Vowel From String

From Dev

How do i remove chars from a string?

Related Related

  1. 1

    How do I remove multiple offending characters from my string?

  2. 2

    How do I remove new line characters from a string?

  3. 3

    How do I remove the first n characters from a string in c?

  4. 4

    Remove last characters from string

  5. 5

    How do I remove the last line of a string that has visible characters in ruby?

  6. 6

    How do I remove multiple characters in a string

  7. 7

    How do I remove unknown characters in a string?

  8. 8

    How do I remove unknown characters in a string?

  9. 9

    How do you remove the first and the last characters from a Multimap's String representation?

  10. 10

    How do I remove the first or last n characters from a value in q/ kdbstudio?

  11. 11

    How to remove last n characters from a string in Bash?

  12. 12

    how to remove first and last characters from exploded php string

  13. 13

    How to remove last 3 characters from a list of String in java?

  14. 14

    Remove the last X specific characters from string

  15. 15

    Bash remove first and last characters from a string

  16. 16

    Remove all the characters from string after last '/'

  17. 17

    Remove Punctuation characters at last from a string

  18. 18

    Remove all the characters from string after last '/'

  19. 19

    Bash remove first and last characters from a string

  20. 20

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

  21. 21

    In an expect script, how do I remove a set of special characters from a string variable?

  22. 22

    How do I remove all non alphanumeric characters from a string except dash?

  23. 23

    How do I remove copyright and other non-ASCII characters from my Java string?

  24. 24

    Remove the last characters of a string

  25. 25

    How do I remove all but the last occurrence of a string?

  26. 26

    how do i remove the first characters of a string [python]

  27. 27

    How do I remove CSS from a string?

  28. 28

    How Do I Remove A Vowel From String

  29. 29

    How do i remove chars from a string?

HotTag

Archive