LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-15-2010, 10:31 PM   #1
ben1173
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Rep: Reputation: 0
Extracting columns


Hi,

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

Thank you
 
Old 10-16-2010, 01:08 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012
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.
 
Old 10-16-2010, 09:25 PM   #3
ndarkduck
LQ Newbie
 
Registered: Nov 2008
Location: Mex,Mex
Distribution: Fedora || Red Hat Linux
Posts: 28

Rep: Reputation: 7
Quote:
Originally Posted by David the H. View Post
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
 
Old 10-17-2010, 04:03 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012
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.
 
Old 10-17-2010, 04:09 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ndarkduck View Post
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
its an easy question, but you make the solution look harder. Lose the cat, its useless
Code:
awk 'NR>1{print $5}' sarout
 
Old 10-18-2010, 11:37 PM   #6
ben1173
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thank you guys. That sloved my problem. I'll be more specific next time.
Thanks again
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting columns from multiple files with awk orcaja Linux - Newbie 7 02-14-2012 11:24 PM
ls columns twelvenine Linux - Newbie 6 12-21-2008 02:33 PM
Need Help With Columns Post Modern Programming 2 02-01-2006 12:18 PM
Vi Columns Chaitanyayardi Linux - Software 5 05-12-2005 08:27 AM
How can I see the other 10 columns sakulagi Linux - Software 1 06-16-2003 10:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:52 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