You are not logged in.

#1 2012-03-31 04:28:27

awayand
Member
Registered: 2009-09-25
Posts: 398

[SOLVED] grep output matching parts only

I want grep to output only what it matched, but according the man page, it will put each matching part on a separate line, which does not work for me. I am matching two numbers, but they don't get output together, but each on a separate line. Any idea how to fix this?

Here is my code:

 [x@arch ~]$ sudo hddtemp /dev/sda|cut -f4 -d" "|grep [[:digit:]+] -o
4
6

Last edited by awayand (2012-03-31 05:08:06)

Offline

#2 2012-03-31 04:38:06

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED] grep output matching parts only

man tr

This might complain about trying to replace something with nothing. If that happens, just replace \n with another something, and then sed can dispose of the new something.

Offline

#3 2012-03-31 04:45:08

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] grep output matching parts only

Or use awk and printf:

awk '{printf "%s ", $3}' <(sudo hddtemp /dev/sda /dev/sdb)

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#4 2012-03-31 05:07:47

awayand
Member
Registered: 2009-09-25
Posts: 398

Re: [SOLVED] grep output matching parts only

genius. thanks, dev/zero!

sudo hddtemp /dev/sda |cut -f4 -d" "|tr -cd [:digit:]

Offline

#5 2012-03-31 05:16:40

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED] grep output matching parts only

awayand wrote:

genius. thanks, dev/zero!

sudo hddtemp /dev/sda |cut -f4 -d" "|tr -cd [:digit:]

Haha, the actual solution you found is more elegant than the one I had in mind!

sudo hddtemp /dev/sda | cut -f4 -d" " | grep [[:digit:]+] -o | tr '\n' ' ' | sed 's| ||g'

Offline

Board footer

Powered by FluxBB