You are not logged in.

#1 2012-11-14 02:24:34

kdar
Member
From: Alabama!!!
Registered: 2009-06-27
Posts: 352

[SOLVED] Extract/Cut values from different columns in output?

I have following output when I run a command below:

$ likwid-powermeter sleep 5 | grep 'Joules\| s'
Runtime: 5.00299 s 
Energy consumed: 17.9783 Joules 

I would like to extract only 5.00299 and 17.9783. How can I do this?

I tried to use awk and cut.. but I could not get it the way I wanted, I could only cut one column.. but those values are two different columns. 5.00299 is in 2nd column on first line and 17.9783 is in 3rd column in the 2nd line.

When I tried to pipe everything to cut I was getting following:

$ likwid-powermeter sleep 5 | grep 'Joules\| s' | cut -d' ' -f3                     
s                                                                                                                   
18.7806  

What can I do?

Last edited by kdar (2012-11-14 06:34:14)

Offline

#2 2012-11-14 02:57:08

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,302
Website

Re: [SOLVED] Extract/Cut values from different columns in output?

awk 'NR==1{print $2} NR==2{print $3}'  < command

You could probably do without the grep as well; awk can handle that...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Online

#3 2012-11-14 03:15:44

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,092
Website

Re: [SOLVED] Extract/Cut values from different columns in output?

awk '{ print $(NF -1) }'

Offline

#4 2012-11-14 03:21:04

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,302
Website

Re: [SOLVED] Extract/Cut values from different columns in output?

falconindy wrote:

awk '{ print $(NF -1) }'

Brutal.


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Online

#5 2012-11-14 03:42:27

kdar
Member
From: Alabama!!!
Registered: 2009-06-27
Posts: 352

Re: [SOLVED] Extract/Cut values from different columns in output?

wow.. thank all. I didn't know how to specify lines in awk.

How does NF -1 work?

Offline

#6 2012-11-14 04:19:20

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,092
Website

Re: [SOLVED] Extract/Cut values from different columns in output?

NF refers to the number of fields in the current line. $NF refers to the value of the last field. so... $(NF - 1) is the value of the next to last field.

Last edited by falconindy (2012-11-14 04:19:50)

Offline

#7 2012-11-14 05:39:54

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 6,790

Re: [SOLVED] Extract/Cut values from different columns in output?

Please mark [solved], as I'm pretty sure it is smile


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

Board footer

Powered by FluxBB