Any way to grep a string in directories and return the result with diskusage aswell?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Any way to grep a string in directories and return the result with diskusage aswell?
# 1  
Old 11-19-2008
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  
Old 11-19-2008
Something like this maybe:

ls -l $DIRECTORY/* | grep "$string" | awk '{print $5" "$9}'
# 3  
Old 11-20-2008
Just tried that, doesn't work, nothing gets printed to the screen Smilie
# 4  
Old 11-20-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Wrong result return from script

Hi Gurus, I need a script to compare two files: sample file like below: list: cde,file4 cde,file5 def,file6 def,file7 def,file8 abc,file1 abc,file2 abc,file3 acd,file9 acd,file10 tmp file1 file2 file3 file4 (12 Replies)
Discussion started by: ken6503
12 Replies

3. Shell Programming and Scripting

Result of Catching Return Value from Sub_script.sh to Main_script.sh is not as Expected

Main_script.sh #! /bin/sh ./Sub_script.sh rc=$? echo "Return code from Sub_script.sh : $rc" if ; then echo "$rc = 991" echo "" exit 1 elif ; then echo "$rc = 992" echo "" exit 1 elif ; then echo "$rc = 0" echo "" exit 1 fi (2 Replies)
Discussion started by: duddukuri
2 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. Shell Programming and Scripting

Grep for a string and then grep using a string from that result

Hello, Thanks in advance for the query. There is a log file abcd.log which has multible line like this. "hello1" , "hello2", "hello3" , "hello4" , "hello5" I want to grep for the lines which has "hello4" & "hello5" and use "hello2" to grep the same log file again. All these should... (8 Replies)
Discussion started by: kzenthil
8 Replies

6. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

7. Solaris

df command on Sol8 machine doesn't return a result

I have a sun4u system running Solaris 8. I tried running the df command but it returns a blank result. Also I'm unable to collect an explorer from this system as the OS complains that the disk is full. What could be going on here? (10 Replies)
Discussion started by: dperry1973
10 Replies

8. UNIX for Dummies Questions & Answers

| help | unix | grep - Can I use grep to return a string with exactly n matches?

Hello, I looking to use grep to return a string with exactly n matches. I'm building off this: ls -aLl /bin | grep '^.\{9\}x' | tr -s ' ' -rwxr-xr-x 1 root root 632816 Nov 25 2008 vi -rwxr-xr-x 1 root root 632816 Nov 25 2008 view -rwxr-xr-x 1 root root 16008 May 25 2008... (7 Replies)
Discussion started by: MykC
7 Replies

9. Shell Programming and Scripting

Pick up the return code for every iteration and display the result only once in loop.

Hi All, I amlearning UNIX scripting. I have a small query. I would be thankful if any one helps me out. I have a below piece of code which delets the files. If file dosent have the permissions to delete a particular file I have used 2>>operator to track the error code. But my objective is... (1 Reply)
Discussion started by: manas6
1 Replies

10. Shell Programming and Scripting

append a string to a grep result

hello, iostat -En | grep Vendor | grep -v DV | awk '{print $1 $2}' | sort -u returns Vendor:HP I want to append Disk to it. i.e.: Disk Vendor:HP how to do that? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies
Login or Register to Ask a Question