LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2011, 08:22 AM   #1
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Rep: Reputation: 47
grep filter within line?


after using grep, I have the following line:
Code:
:Temperature 49
how to filter it out some more, so that only '49' is left?
thanks.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-10-2011, 08:34 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can either pipe the output to another grep to match exactly the number at the end of the line and using the -o option to print out only the matching part:
Code:
grep Temperature file | grep -E -o [0-9]+$
To avoid the double grep command, you can use awk (or even sed) to match the Temperature pattern and extract only the information you need:
Code:
awk '/:Temperature/{print $NF}' file
 
2 members found this post helpful.
Old 10-10-2011, 11:44 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Or use sed:

Code:
sed 's/^:Temperature \([0-9]\+\)$/\1/'
This will eliminate the need for the first grep command, too.
 
1 members found this post helpful.
Old 10-10-2011, 12:14 PM   #4
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,198

Rep: Reputation: 47
You can also use 'cut' command:

$ echo ":temperature 49" | cut -d" " -f2
49
 
Old 10-11-2011, 02:32 AM   #5
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Original Poster
Rep: Reputation: 47
thanks all.

er, there are actually two lines with :Temperature, and I'd like to merge both numbers into one line.
is there a way to combine these two awk into single awk:

Code:
 awk '/:Temperature/{print $2}' | awk '{ printf "%s ", $0 }'
(I plan to use it in cron script and wish to reduce resource usage as much as possible)
or some better way?

Last edited by qrange; 10-11-2011 at 02:34 AM.
 
Old 10-11-2011, 02:34 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by qrange View Post
er, there are actually two lines with :Temperature, and I'd like to merge both numbers into one line.
Of course. Please show us the two lines and the desired result.
 
Old 10-11-2011, 02:43 AM   #7
qrange
Senior Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable/testing, amd64
Posts: 1,061

Original Poster
Rep: Reputation: 47
nevermind, I just found out how to do it:
awk '/:Temperature/{printf "%s ", $2}'


the two lines are these:
:Temperature 48
:Temperature 25

and output should be:
48 25


thanks.
 
Old 10-11-2011, 02:51 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
That's it. You may want to add a terminating newline:
Code:
awk '/:Temperature/{printf "%d ", $NF} END{printf "\n"}' file
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
filter source line based on results line in log using awk and sed samanp Programming 5 04-06-2011 09:42 AM
[SOLVED] bash: using grep to filter 25 or above schneidz Programming 2 12-04-2010 02:33 AM
[SOLVED] Filter through line/s to grab specific fields/data in the line with example shayno90 Linux - Newbie 11 10-14-2009 11:51 AM
Can grep filter out words? extrasolar Linux - General 1 07-20-2006 03:14 PM
Grep-like filter exists? carl.waldbieser Programming 3 08-31-2005 11:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration