UNIX BASH:从字符串中提取数 [英] UNIX BASH: extracting number from string
问题描述
这可能是一个很简单的问题,以有经验的人在UNIX但是我想从字符串中提取电话号码,并不断得到错误的结果。
This is probably a very simple question to an experienced person with UNIX however I'm trying to extract a number from a string and keep getting the wrong result.
这是字符串:
8962 ? 00:01:09 java
这它的输出我想要
8962
但由于某些原因,我不断收到完全相同的字符串返回。这是我试过
But for some reason I keep getting the same exact string back. This is what I've tried
pid=$(echo $str | sed "s/[^[0-9]{4}]//g")
如果有人可以帮助我走出这将是AP preciated。
If anybody could help me out it would be appreciated.
推荐答案
有对皮肤猫不止一种方法:
There is more than one way to skin a cat :
pti@pti-laptop:~$ echo 8962 ? 00:01:09 java | cut -d' ' -f1
8962
pti@pti-laptop:~$ echo 8962 ? 00:01:09 java | awk '{print $1}'
8962
切削减了基于一个分隔符不同领域的一条线或只是字节范围,并往往是这些任务非常有用。
cut cuts up a line in different fields based on a delimeter or just byte ranges and is often useful in these tasks.
AWK是一个较旧的编程语言用于一次操作的东西一行特别有用。
awk is an older programming language especially useful for doing stuff one line at a time.
这篇关于UNIX BASH:从字符串中提取数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!