LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-30-2010, 05:52 AM   #1
Tux Rules
Member
 
Registered: Feb 2009
Location: at my place
Distribution: Arch
Posts: 153

Rep: Reputation: 25
in need for an advice regarding awk command


I've just discovered this cool nifty and handy command that I think would help me a lot, but something it's not really clear to me though I've read some tutorials about it. I know if you wanna print the info of a column using awk you refer to that column using the dollar sign $, but I didn't get it how you can refer to a row or how can you refer to a certain value that it's situated in a matrix.

For example if I do df -H this pops out in my terminal
Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda10             48G   13G   33G  28% /
tmpfs                1006M   12K 1006M   1% /lib/init/rw
udev                 1001M  288K 1001M   1% /dev
tmpfs                1006M  524K 1006M   1% /dev/shm
/dev/sda6              98G   77G   22G  79% /mnt/WD_1
/dev/sda7              98G   26G   73G  26% /mnt/WD_3
/dev/sda5              98G   53G   45G  55% /mnt/WD_kit
/dev/sda1              76G   45G   31G  60% /mnt/sda1
/dev/sda9              49G   27G   23G  55% /mnt/sda9
If I do df -H|awk '/ / {print $3}' it will show me the used space column
Code:
Used
14G
13k
295k
537k
83G
28G
57G
49G
29G
Ok now how can I make awk so it will diplay only the 26G used space of my sda7 without involving grep command 'cause I know another way it would be something like df -H|grep sda7|awk '/ / {print $3}', but because I need to get these values so I could perform calculations with them inside awk involving grep will only make things harder and awk-ard 8-)

So anybody has any idea how can I refer to a specific value from a matrix like display, using awk ?

Last edited by Tux Rules; 10-30-2010 at 05:54 AM.
 
Old 10-30-2010, 05:58 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
awk '/sda7/ {print $3}'

Look at using regular expressions with (g)awk: http://www.gnu.org/manual/gawk/gawk.html#Regexp-Usage
 
1 members found this post helpful.
Old 10-30-2010, 06:36 AM   #3
Tux Rules
Member
 
Registered: Feb 2009
Location: at my place
Distribution: Arch
Posts: 153

Original Poster
Rep: Reputation: 25
Quote:
Originally Posted by Nylex View Post
awk '/sda7/ {print $3}'

Look at using regular expressions with (g)awk: http://www.gnu.org/manual/gawk/gawk.html#Regexp-Usage
thx man I'm new to all the awk-enish and that was pretty handy, 'cause i can see actually awk has grep implementated inside it.

I needed awk to display the used and fre space of my NTFS partitions that are mounted in the /mnt folder. Now I can accomplish that with the command

Code:
df -h|awk '/mnt/ {{used += $3} {free += $4}} END {{print used} {print free}}'

Last edited by Tux Rules; 10-30-2010 at 06:53 AM.
 
Old 10-30-2010, 07:12 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Just an aside note: you don't need so many brackets. In order to have multiple statements on the same line, you can separate them by semi-colon:
Code:
awk '/mnt/{used += $3; free += $4} END {print used; print free}'
Apart from various awk tutorials, the best reference is the official documentation, here. Enjoy awk!
 
1 members found this post helpful.
  


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
About awk command incomingid Linux - Newbie 5 04-01-2009 10:44 PM
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
awk command toaravind Linux - General 5 07-02-2008 01:25 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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