LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2011, 10:38 AM   #1
mcgregor
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Rep: Reputation: 0
BASH Scripting: Cutting specific info from df command


Hello. I'm relatively new to Bash scripting. I have to create a table that lists specific things from the output of the df command.
It has to sort of look like this:
Filesystem Mounted Size Free Space
/dev/sda1 / 20G 9.7

and so on so forth.

I tried
df -h | tr -s " " | cut -f1 -d " "
but that gives me
/dev/sda1
/dev/sda1
and so on so forth

I understand that the cut command is seeing each column of info as one field. Is there a way around this? I need to cut each piece of info for one line
so
echo -e " $( df -h | tr -s " " | cut -f1 -d " ") \t $( df -h | tr -s " " | cut -f2 -d " ") \t
etc etc etc

Now here's the kicker. I can't use awk or sed. In my assignment, we are specifically forbidden to use awk or sed, so this implies to me that there is a way to do it. Hopefully this is enough information to make sense of what I need help with.
 
Old 04-19-2011, 10:50 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I guess the first question would be do you have to use cut?
Also, how are you planning on getting data from each row?

Are you going to feed the information into a loop? If so, you could get rid of cut and tr all together.

And finally, yes awk and sed would make short work of this problem
 
Old 04-19-2011, 10:58 AM   #3
mcgregor
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Well I just assumed I would cut to get specific info from df. I want to try and echo each specific piece, in an specific order, instead of echoing the whole column, which is what's happening right now. I need to isolate each piece

It's got to look sort of like a table that unfortunately doesn't look like the table of the df command. So I just assumed this meant that I have to cut specific info.
 
Old 04-19-2011, 11:25 AM   #4
mcgregor
LQ Newbie
 
Registered: Apr 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Code:
df -h | grep ^/dev | tr -s " " | while read next
do
	device=$(echo $next | cut -f 1 -d " ")
	mntpoint=$(echo $next | cut -f 6 -d " ")
	size=$(echo $next | cut -f 2 -d " ")
	freespace=$(echo $next | cut -f 4 -d " ")
	echo -e "$device \t $mntpoint \t $size \t $freespace"
done
Apparently this is the answer. My teacher just gave it to me. Ugh. I've spent hours on this and I was going the wrong away about it entirely
 
Old 04-19-2011, 11:46 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well that is an answer. Personally not my choice as later when you wish to use a variable inside the loop and then report on it later, the above format will fail

Plus why not just use the read part of the loop to do all the work for you
Code:
#!/bin/bash

(

echo "Filesystem|Mounted|Size|Free Space"

while read -r device size _ freespace _ mntpoint
do
    [[ $device =~ ^/dev ]] || continue
    echo "$device|$mntpoint|$size|$freespace"
done< <(df -h)

) | column -t -s "|"
 
  


Reply



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
bash scripting...A command that can stop others ntelos Linux - Newbie 6 03-31-2010 05:14 PM
Bash scripting/command - get the device currently on? coyttl Linux - Newbie 9 12-30-2008 04:24 PM
Screen Command with Bash Scripting inthecrossfire Linux - Software 5 07-15-2008 01:18 PM
Vi question. Cutting a specific lines blackzone Linux - General 1 07-19-2006 06:10 PM
Bash command scripting otisthegbs Linux - General 6 07-06-2004 12:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

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