1

I have been teaching myself bash scripting and I wanted to write a script that makes a txt file with specific hardware information. I figured that 'grep' would be a great tool for pulling the info I wanted from /proc and other places.

My issue is the output is doubled. What am I doing wrong?

$ grep "Mhz" /proc/cpuinfo
cpu MHz     : 1200.000
cpu MHz     : 1200.000

$ cat /proc/cpuinfo | grep "MHz"
cpu MHz     : 1200.000
cpu MHz     : 1200.000

....I do understand that the commands above will not redirect into my txt file. I am trying to get the command to output what I want before redirecting.

1 Answer 1

4

You have two CPUs, or at least two cores, or at least two threads. Ubuntu sees these as two distinct processors and /proc/cpuinfo reflects that.

You can artificially limit this to only pull the first match:

$ grep -m1 "[MG]Hz" /proc/cpuinfo
model name  : Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz

I don't understand the second part of your question. If you want to display that and write it to a file, use tee:

$ grep -m1 "[MG]Hz" /proc/cpuinfo | tee testfile
model name  : Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz

$ cat testfile 
model name  : Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz

If you just want to write it to a file, just redirect it with >. Grep outputs matches to STDOUT, so that will redirect just fine.

1
  • Thank you for your help. I think I got it working the way I want. Nov 25, 2015 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.