0

I am writing a csh script and need to assign the numerical value in a string to a variable

Here is an example of the string value: "pkt_size=78"

The characters in the string will always be the same except for the number.

I pretty much need to extract the value after the "=" sign.

What Unix command can I use to do this?

Thanks,

chepner
  • 497,756
  • 71
  • 530
  • 681
user1526912
  • 15,818
  • 14
  • 57
  • 92

4 Answers4

3
% set var = pkt_size=78
% set num = `echo $var | sed 's/.*=//'`
% echo $num
78

Or if you're using tcsh (not plain csh) and the prefix is a fixed string:

% set var = pkt_size=78
% set num = $var:s/pkt_size=//
% echo $num
78

Obligatory reference.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

Quick hack...

$ NUMBER=`echo pkt_size=78 | sed -e 's/pkt_size=\(\d*\)/\1/'`
Andy Jones
  • 6,205
  • 4
  • 31
  • 47
0

According to the C-shell Cookbook

The C-shell has no string-manipulation tools. Instead we mostly use the echo command and awk utility. The latter has its own language for text processing and you can write awk scripts to perform complex processing, normally from files. There are even books devoted to awk such as the succinctly titled sed & awk by Dale Dougherty (O'Reilly & Associates, 1990). Naturally enough a detailed description is far beyond the scope of this cookbook.

An awk hack would be like this:

set var=`echo "$var" | awk -F= '{print $2}'`
user000001
  • 32,226
  • 12
  • 81
  • 108
0

If you must use csh, you can use the built-in modifiers.

% set v=pkt_size=78
% echo $v:s/pkt_size=//
78

This may be specific to tcsh.

But there's really lots of reasons to use a bourne shell derivative (sh, ksh, bash) instead.

evil otto
  • 10,348
  • 25
  • 38