6

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.

14

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.

10

You actually want to remove the trailing 11 characters from the string; here's another way to do it:

$ var=type_cardio_10-11-2017
$ var2=${var%???????????}
$ echo "$var2"
type_cardio
6

Another approach in bash:

echo "${var::-10}"

Or in older versions:

echo "${var::${#var}-10}" #or
echo "${var: : -10}"
4

1) bash solution:

var1="type_cardio_10-11-2017"
var2=${var1%_*}

2) cut solution:

var1="type_cardio_10-11-2017"
var2=$(cut -d'_' -f1,2 <<<"$var1")
echo "$var2"

The output:

type_cardio
  • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ? – Rak kundra Oct 20 '17 at 17:44
  • 1
    @SanthoshPogaku, that was not clear from your initial question, you have my update – RomanPerekhrest Oct 20 '17 at 17:52
  • Yeah sir, I got what am i looking for . Thank's for your valuable time. – Rak kundra Oct 20 '17 at 17:53
1

Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.

The main part is removing those 10 characters. There's number of ways to extract the part you want from var:

  • printf (pretty portable, doesn't rely on specific shell)

    $ printf "%.11s\n"  "$var"                                                                                                                                                                      
    type_cardio
    
  • awk

    $ var=type_cardio_10-11-2017                                                                                                                                                                    
    $ awk -v awk_var="$var"  'BEGIN{print substr(awk_var,0,length(awk_var)-11)}'                                                                                                                    
    type_cardio
    

    or you can use printf trick here as well:

    $ echo "$var" | awk '{printf "%.11s\n",$0}'                                                                                                                                                     
    type_cardio
    
  • egrep

    $ echo "$var" | egrep  -o '^.{0,11}'                                                                                                                                                           
    type_cardio
    
  • perl:

    $ echo "$var" | perl -lne 'print substr($_,0,11)'                                                                                                                                               
    type_cardio
    
  • python:

    $ python -c 'import sys;print sys.argv[1][0:11] ' "$var"                                                                                                                                        
    type_cardio
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.