![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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? |
|
||||
|
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 |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|