在String(shell / AIX)中查找并打印出特定字符的出现次数

时间:2016-10-19 14:11:43

标签: shell sed grep aix


我有以下问题。在shell脚本中,我需要找出String中多个点('。')的位置。

我找到了一些解决方案,但问题是,它们可以在Linux shell上运行,但不能在AIX shell上运行。

例:
字符串看起来像这样:
public static class constants { public static List<string> BannedUsernames = new List<string>() { "admin", "superadmin" // whatver usernames you want to block }; }
(是的,它是一个文件名)

我用grep试了一下: someString-10.12.16.somestring.sql
但是AIX下的grep并不支持选项-ob

我用sed尝试了它:echo $string | grep -aob '\.' | sed -n '2p' | cut -c 1-2)
但在那里,我收到错误消息:sed 's/\([^\.]*\..*\)\..*/\1/' | wc -c

有人能给我一个在AIX shell中运行的解决方案吗? 对于每个点,一个命令返回该点的位置。

谢谢: - )

1 个答案:

答案 0 :(得分:0)

你可以尝试这些;

返回第一个点的位置:(索引从1开始而不是0;)

 echo `expr match "someString-10.12.16.somestring.sql " '[a-z:A-Z:0-9:-]*.'`

第二个点;

echo `expr match "someString-10.12.16.somestring.sql " '[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.'`

第三个点;

echo `expr match "someString-10.12.16.somestring.sql" '[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.'`

最后一个点;

echo `expr match "someString-10.12.16.somestring.sql" '[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.'`

例如

user@host:>echo `expr match "someString-10.12.16.somestring.sql" '[a-z:A-Z:0-9:-]*.'` &&
> echo `expr match "someString-10.12.16.somestring.sql" '[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.'` &&
> echo `expr match "someString-10.12.16.somestring.sql" '[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.'` &&
> echo `expr match "someString-10.12.16.somestring.sql" '[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.[a-z:A-Z:0-9:-]*.'`

14
17
20
31


user@host:>echo `expr match "123.567.90." '[0-9]*.'`
4
user@host:>echo `expr match "123.567.90." '[0-9]*.[0-9]*.'`
8
user@host:>echo `expr match "123.567.90." '[0-9]*.[0-9]*.[0-9]*.'`
11