Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用shell脚本查找/获取文件中的编号_Linux_Bash_Shell_Ubuntu - Fatal编程技术网

Linux 如何使用shell脚本查找/获取文件中的编号

Linux 如何使用shell脚本查找/获取文件中的编号,linux,bash,shell,ubuntu,Linux,Bash,Shell,Ubuntu,我不熟悉Linux Shell脚本 据我所知,在文件中使用find number可以通过grep完成 egrep -o "[0-9][0-9]*" my_file 但是我如何得到这些字符串的第一个数字并使其成为一个统计数据呢。。 比如12341231267所以我三次得到1 我知道使用 A=$(tr -cd 1 < page.html|wc -c) A=$(tr-cd1

我不熟悉Linux Shell脚本 据我所知,在文件中使用find number可以通过grep完成

egrep -o "[0-9][0-9]*" my_file
但是我如何得到这些字符串的第一个数字并使其成为一个统计数据呢。。 比如12341231267所以我三次得到1

我知道使用

A=$(tr -cd 1 < page.html|wc -c)
A=$(tr-cd1
可以在文件中获取数字“1”计数,但这不是我想要的。。。。 我想数一数第一位数字“1”。。。。。。 这就是为什么对我来说很难

请帮忙。。。。。。 非常感谢

A=$(egrep -o '[0-9]+' my_file | egrep -c '^1')

第一个
egrep
查找所有数字并输出它们。第二个
egrep
使用
-c
选项输出匹配计数,regexp匹配以
1

开头的行。从问题来看,文件似乎包含各种字符,您希望隔离文件中所有数字的第一位数字。看起来数字不必是一行中的第一个单词(比如前面没有空格)。记住这两个假设,您可以执行以下操作:

grep '[0-9]' test.html| sed 's/\([0-9]\+\)/\n\1\n/g' |grep '^[0-9]' |cut -c1 |sort |uniq -c
例如:

curl -N -s 'http://stackoverflow.com/users/1353267/samveen' |grep '[0-9]' |sed 's/\([0-9]\+\)/\n\1\n/g' |cut -c1 |grep '^[0-9]' |sort |uniq -c
重要:在上面的示例页面中,有一行
{“fkey”:“8f1a9c6e21503516793b853265ec4939”,“isRegistered”:true,“userId”:1353267,“accountId”:1430801,“gravatar”:“
,将其划分如下:

{"fkey":"
8
f
1
a
9
c
6
e
21503516793
b
853265
ec
4939
","isRegistered":true,"userId":
1353267
,"accountId":
1430801
,"gravatar":"<div class=\"\">
此外,如果巧妙地选择了
sed
转换,则不需要cut命令。也就是说,如果
\([0-9]\+\)
模式的一部分更改为
\([0-9]\)[0-9]*
,然后sed将只显示每个数字的第一位数字,而不是整数。因此不再需要
剪切-c1
。使用
sed的/\b\([0-9]\)[0-9]*\b/\n\1\n/g'
,我们得到:

{"fkey":"8f1a9c6e21503516793b853265ec4939","isRegistered":true,"userId":
1
,"accountId":
1
,"gravatar":"<div class=\"\">
{“fkey”:“8f1a9c6e21503516793b853265ec4939”,“isRegistered”:true,“userId”:
1.
,“帐户ID”:
1.
,“gravatar”:
因此,不需要削减


提供有关输入文件的更多信息,可以进一步优化该命令。

提供文件和预期输出的示例数据。哦,天哪……这真的很有帮助……我使用A=$(egrep-o“[0-9][0-9]*”page.html | egrep-c'^1'))但是,如果我可以将所有数字(包括浮点数)进行grep,该怎么办呢?请参阅,以获取将浮点数与regexp匹配的信息,我只需在SO搜索栏中键入
regex floating point
,即可找到它。哇……更好的答案……但我只需要输出计数,不知道如何操作。。。。
{"fkey":"8f1a9c6e21503516793b853265ec4939","isRegistered":true,"userId":
1
,"accountId":
1
,"gravatar":"<div class=\"\">