Uthman
Uthman

Reputation: 9837

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

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 integer is there in front of the line that starts with "number"?

Thanks...

Upvotes: 0

Views: 689

Answers (4)

ghostdog74
ghostdog74

Reputation: 343067

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

Upvotes: 2

Morten Siebuhr
Morten Siebuhr

Reputation: 6108

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.

Upvotes: 1

Loki Astari
Loki Astari

Reputation: 264699

Use awk:

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

Upvotes: 2

martin.malek
martin.malek

Reputation: 2218

use grep and cut, this will return only the number

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

Upvotes: 1

Related Questions