The UNIX and Linux Forums  

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 01:37 AM
append a string to a grep result melanie_pfefer Shell Programming and Scripting 8 03-19-2008 06:19 AM
grep to handle a 0 result ocelot UNIX for Dummies Questions & Answers 6 02-05-2007 10:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-19-2008
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?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2008
Autocross.US's Avatar
Registered User
 

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

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

Join Date: Nov 2008
Posts: 2
Just tried that, doesn't work, nothing gets printed to the screen
Reply With Quote
  #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,471
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 04:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66