views:

74

answers:

4

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...

+1  A: 

use grep and cut, this will return only the number

cat ./file.txt | grep number | cut -d " " -f 2
martin.malek
no need cat. grep number file.txt | .....
ghostdog74
+2  A: 

Use awk:

cat ./file.text | awk '/number/ {print $2}'
Martin York
no need cat . awk '.....' file.txt
ghostdog74
I prefer using cat, makes the command more readable, and portable. You can just replace the cat with whatever generates the output you want to parse.
Andre Miller
with cat, you add 3 more characters. Plus, you create one extra pipe process. Portable?? in what sense? And providing the file to awk's input is not/or less portable?
ghostdog74
+1  A: 

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.

Morten Siebuhr
if its a large file, use just awk!. cat + grep + cut +whatever is expensive operation on large files. Also, there is no need for redirection. just pass the file directly to awk.
ghostdog74
+2  A: 
awk '$1=="number"{print $2}' file
ghostdog74
Thanks man. Does the command mean to print field 2 where field 1 == number?
baltusaj
yes that's right.
ghostdog74