LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Extracting columns (https://www.linuxquestions.org/questions/linux-general-1/extracting-columns-838416/)

ben1173 10-15-2010 10:31 PM

Extracting columns
 
Hi,

How can I extract 5th column from a file without the header. Please help me....

Thank you

David the H. 10-16-2010 01:08 AM

For simple extraction you can usually use cut, and for more complex jobs you'd probably want to use awk.

Please give us a real-life example of the file contents, along with how you want the output to look, if you want any detailed advice.

ndarkduck 10-16-2010 09:25 PM

Quote:

Originally Posted by David the H. (Post 4129215)
For simple extraction you can usually use cut, and for more complex jobs you'd probably want to use awk.

Please give us a real-life example of the file contents, along with how you want the output to look, if you want any detailed advice.

Come on! this is a easy one, put an example just for googlers sake. I'll use my own example.


Code:

cat sarout
Linux 2.6.32-gentoo-r7 (GUNGNER)        10/16/10        _i686_        (8 CPU)

20:20:21        CPU    %user    %nice  %system  %iowait    %steal    %idle
20:20:22        all      2.15      0.00      1.01      0.13      0.00    96.72
20:20:23        all      1.82      0.00      0.97      0.00      0.00    97.21
20:20:24        all      1.90      0.00      0.88      0.00      0.00    97.22
20:20:25        all      1.99      0.00      0.99      0.00      0.00    97.02

If I want to get the 5th column of this.
I'll use
Code:

cat sarout | awk '{print $5}'
_i686_

%system
1.01
0.97
0.88
0.99

For truncating the header there are a lot of options, I'll show you the grep one.
Code:

cat sarout | awk '{print $5}' | grep -E "[0-9]\.[0-9]{2}"
1.01
0.97
0.88
0.99

Done :D

David the H. 10-17-2010 04:03 AM

Well, sure, I could've just posted something like that. ;) But I think it's important for posters to learn how to define their questions clearly and with enough detail up-front. I don't enjoy having to guess about the poster's situation or asking a lot of fishing questions.

I also like to encourage them to do their own research, which is why I mentioned which tools are most likely to be useful.

In short, vague questions generally get vague answers. Detailed posts are more likely to draw out helpful responses, and get them faster. :twocents:

ghostdog74 10-17-2010 04:09 AM

Quote:

Originally Posted by ndarkduck (Post 4129946)
For truncating the header there are a lot of options, I'll show you the grep one.
Code:

cat sarout | awk '{print $5}' | grep -E "[0-9]\.[0-9]{2}"
1.01
0.97
0.88
0.99

Done :D

its an easy question, but you make the solution look harder. Lose the cat, its useless
Code:

awk 'NR>1{print $5}' sarout

ben1173 10-18-2010 11:37 PM

Thank you guys. That sloved my problem. I'll be more specific next time.
Thanks again


All times are GMT -5. The time now is 06:35 AM.