开发者

Extracting an integer from a line with the help of linux script?

开发者 https://www.devze.com 2022-12-12 00:33 出处:网络
I have a file, one of whose line contains: number8 how can i use sed, grep or whatever linux script to find out what intege开发者_如何转开发r is there in front of the line that starts with \"numbe

I have a file, one of whose line contains:

number 8

how can i use sed, grep or whatever linux script to find out what intege开发者_如何转开发r is there in front of the line that starts with "number"?

Thanks...


awk '$1=="number"{print $2}' file


Use awk:

cat ./file.text | awk '/number/ {print $2}'


use grep and cut, this will return only the number

cat ./file.txt | grep number | cut -d " " -f 2


Another way is to use awk:

awk '/number/ {print $2}' < ./file.txt

It's a single command, which some prefer. If it's a large file, you may prefer the cat | grep | cut-way, as the three programs run in separate processes.

0

精彩评论

暂无评论...
验证码 换一张
取 消