The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pick up the return code for every iteration and display the result only once in loop. manas6 Shell Programming and Scripting 1 10-21-2008 07:12 AM
How to negate grep result? mmdawg Shell Programming and Scripting 4 05-05-2008 08:24 AM
diaplaying the grep result rag84dec Shell Programming and Scripting 1 03-27-2008 02:37 AM
append a string to a grep result melanie_pfefer Shell Programming and Scripting 8 03-19-2008 07:19 AM
grep to handle a 0 result ocelot UNIX for Dummies Questions & Answers 6 02-05-2007 11:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-19-2008
Eddeh Eddeh is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Any way to grep a string in directories and return the result with diskusage aswell?

What Im basically trying to do is this:

I have a small script that can grep any parameter entered into a search string, then print to the screen the name of each file the parameter appears in as well as the file path, ie the directory.

The code Im using just for this is....

Directory
---------
1. Search /export/home/btch1/nelse2
Enter Choice number ( press q to quit ) :\c"
read choice
case $choice in
1)

echo "------------------------------"
echo "Searching /xxxx/xxxx"
echo "------------------------------"
echo $string
grep -li "$string" $DIRECTORY/*
echo "--------------------------------------"
echo " Displaying directory size"
echo "--------------------------------------"
df -k .
;;
*)


Which brings up the results as follows

Searching /xxxx/xxxx
------------------------------
2005
/export/home/btch1/nelse2/PR_MX_INT_0001_20080917180857.dat
/export/home/btch1/nelse2/Search2.ksh
--------------------------------------
Displaying directory size
--------------------------------------
Filesystem kbytes used avail capacity Mounted on
/dev/vx/dsk/bootdg/rootvol
10080200 7323251 2656147 74% /


What I would like though is for the 2 resulting files displayed to have thier filesize before or after aswell, for example

1288 /export/home/btch1/nelse2/Search2.ksh

ive tried putting du before the grep and piping to the rest of the code, but it either doesnt work or just prints the filesize and not the file name, or just the filesize and not the filename, so the likes of

du - sk | grep -li "$string" $DIRECTORY/*

doesn't work

Can anyone help?
  #2 (permalink)  
Old 11-19-2008
Autocross.US's Avatar
Autocross.US Autocross.US is offline
Registered User
  
 

Join Date: Nov 2008
Location: Chesapeake, VA
Posts: 73
Something like this maybe:

ls -l $DIRECTORY/* | grep "$string" | awk '{print $5" "$9}'
  #3 (permalink)  
Old 11-20-2008
Eddeh Eddeh is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Just tried that, doesn't work, nothing gets printed to the screen
  #4 (permalink)  
Old 11-20-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
The reason why this doesn't work is simple: in "ls -l $DIR | grep ..." the grep works on the output of ls, not on the files named in this output.

Note, that getting the diskspace and grepping for some content are two entirely different functions. Therefore you could do it only by performing these two different functions on every file and binding together the output of these via a script.

Having said this: use "find" ("man find") to set up a loop and use the "-exec" clause of "find" to 1.) grep the file for the content you are interested in and 2.) use "du" to get the filesize. 3.) Print out both if the grep has found the content, else do nothing. This will give you a list of filenames and -sizes.

I hope this helps.

bakunin
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0