LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 09:22 AM   #1
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable, amd64
Posts: 805

Rep: Reputation: 32
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, 09:34 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 12:44 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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, 01:14 PM   #4
rng
Senior Member
 
Registered: Aug 2011
Posts: 1,011

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

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

Original Poster
Rep: Reputation: 32
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 03:34 AM.
 
Old 10-11-2011, 03:34 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 03:43 AM   #7
qrange
Member
 
Registered: Jul 2006
Location: Belgrade, Yugoslavia
Distribution: Debian stable, amd64
Posts: 805

Original Poster
Rep: Reputation: 32
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, 03:51 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:42 AM
[SOLVED] bash: using grep to filter 25 or above schneidz Programming 2 12-04-2010 03:33 AM
Filter through line/s to grab specific fields/data in the line with example shayno90 Linux - Newbie 11 10-14-2009 12:51 PM
Can grep filter out words? extrasolar Linux - General 1 07-20-2006 04:14 PM
Grep-like filter exists? carl.waldbieser Programming 3 09-01-2005 12:34 AM


All times are GMT -5. The time now is 10:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration