I am looking for extracting some of the string by counting the letter from the beginning in csh shell. For example,
set a = "hello 911 is not 91 only"
I want to extract letter from position 8 to 10 which is 911, similar to a(8:10). Similarly, 20 to 21 for 91. How is it possible?
Thanks for your help. Raj
csh
does not have string functions. UNIX traditionally uses small and simple programs. Selecting data (columns or words) can be done using (for example) sed
, awk
or cut
. In this case one solution using cut
would be:
% set a = "hello 911 is not 91 only"
% echo $a
hello 911 is not 91 only
% echo $a | cut -c 7-9
911
% echo $a | cut -c 18-19
91
Note that your columns are off a bit, 911 is indeed at the 7th to 9th characters.
Or if you want to have the result in a variable:
% set a = "hello 911 is not 91 only"
% set b = `echo $a | cut -c 7-9`
% set c = `echo $a | cut -c 18-19`
% echo "I found $b and $c"
I found 911 and 91