Home > Enterprise >  How to use grep to print specific words only
How to use grep to print specific words only

Time:07-21

I have a variable that contains a string-

$CCSR = "branches/features/arm_and_musl"

I want to pass only the "arm_and_musl" part to a variable, while excluding "branches/features", so something like this-

def dirname= sh " echo $CCSR | grep ????? "

But the main issue is that I don't want to explicitly mention "arm_and_musl" part, I want it to just exclude "branches/features" and print the remaining part whatever that may be.

I'm not sure what to put here so that only the part I want is passed to the variable.

Could you please suggest any solutions for this?

CodePudding user response:

You could use something like this:

echo ${CCSR##*/}

to just output the needed section or if you need the variable to contain it use:

CCSR=${CCSR##*/}

Edit: Since for some reason author was getting an error, thought i would include a solution with sed:

echo $CCSR | sed "s:.*/::"

CodePudding user response:

Just use basename:

#!/bin/sh

CCSR="branches/features/arm_and_musl"
file=$(basename "$CCSR")

echo "$file"
  • Related