简体   繁体   English

借助Linux脚本从一行中提取整数?

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

I have a file, one of whose line contains: 我有一个文件,其中一行包含:

number 8 8号

how can i use sed, grep or whatever linux script to find out what integer is there in front of the line that starts with "number"? 我如何使用sed,grep或任何Linux脚本找出以“ number”开头的行前面存在什么整数

Thanks... 谢谢...

使用awk:

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

使用grep并剪切,这将仅返回数字

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

Another way is to use awk : 另一种方法是使用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 如果文件很大,您可能更喜欢cat | grep | cut cat | grep | cut cat | grep | cut -way, as the three programs run in separate processes. cat | grep | cut ,因为这三个程序在单独的进程中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM