通过数数提取字母
extract letters by counting them
我正在寻找通过计算 csh shell 中从头开始的字母来提取一些字符串的方法。例如,
设置一个="hello 911 is not 91 only"
我想提取8到10位的字母911,类似于a(8:10)。同样,20到21为91。这怎么可能?
感谢您的帮助。
拉吉
csh
没有字符串函数。 UNIX 传统上使用小而简单的程序。可以使用(例如)sed
、awk
或 cut
来选择数据(列或词)。在这种情况下,一种使用 cut
的解决方案是:
% 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
请注意,您的列有点偏离,911 确实在第 7 到第 9 个字符。
或者,如果您想将结果保存在变量中:
% 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
我正在寻找通过计算 csh shell 中从头开始的字母来提取一些字符串的方法。例如,
设置一个="hello 911 is not 91 only"
我想提取8到10位的字母911,类似于a(8:10)。同样,20到21为91。这怎么可能?
感谢您的帮助。 拉吉
csh
没有字符串函数。 UNIX 传统上使用小而简单的程序。可以使用(例如)sed
、awk
或 cut
来选择数据(列或词)。在这种情况下,一种使用 cut
的解决方案是:
% 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
请注意,您的列有点偏离,911 确实在第 7 到第 9 个字符。
或者,如果您想将结果保存在变量中:
% 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