1

I have a variable storing "ping_abc", how do I remove the "ping_" part to get just "abc"?

My approach: variable 'test' stores "ping_abc"

I tried:

echo ${test:0:4}

But this is giving me:

Bad : modifier in $ (4), error

Approach on suggested answer by @Yaron:

set test = "ping_abc"
echo test | awk -F_ '{print $2}'
echo ping_abc | awk -F_ '{print $2}'

o/p:

        //blankspace
 abc

Why does the test variable not get truncated?

terdon
  • 100,812

2 Answers2

2

In tcsh, you can use "history style" : modifiers in parameter expansions:

xenial-vm:~> set test = ping_abc
xenial-vm:~> echo $test
ping_abc
xenial-vm:~> echo ${test:s/ping_//}
abc
xenial-vm:~>

See for example History Substitution

steeldriver
  • 136,215
  • 21
  • 243
  • 336
0

You can use awk in the following way:

echo ping_abc | awk -F_ '{print $2}'

which result with:

abc

Explanation about awk parameters:

  • -F_ - use _ for the input field separator
  • print $2 - print the second field in the input record

More info man awk

Yaron
  • 13,173