The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reg: Search for a directory Rajanikanth Shell Programming and Scripting 8 01-10-2008 02:06 AM
Reg: Search for a directory Rajanikanth UNIX for Dummies Questions & Answers 1 01-07-2008 02:36 AM
Reg: Search for a directory Rajanikanth Shell Programming and Scripting 0 01-07-2008 02:30 AM
Need to search and replace in multiple files in directory hierarchy umen Shell Programming and Scripting 3 12-24-2007 12:56 AM
search for certain word in a files from directory legato UNIX for Dummies Questions & Answers 1 10-31-2006 02:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-17-2004
Registered User
 

Join Date: Sep 2003
Posts: 13
search all files and sub directory

I wanted to search in all the sub directories under /vob/project (recurse) in everything inside /vob/project.

search.run

for x in `cat search.strings`
do
find /vob/project -type f -print | xargs grep -i $x > ~/$x.txt
done


search.string
hello
whoami

I am getting the error
Maximum line length of 2048 exceeded.

Thanks for any help
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-17-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
The message is giving you a clue here. Is there any line in any file under /vob/project that contains a line longer than 2048 chars, which is a limit for some Unix commands?

Try this to count the length of lines in your files
Code:
#!/bin/sh

for file in "$@"; do
  echo -e "\nFILE: $file"
  awk '{printf( "%5d: %s\n", length( $0 ), $0 ) }' $file
done
Save this as linelength.sh, make it executable, and run it as
$ linelength /vob/project/*

Cheers
ZB
Reply With Quote
  #3 (permalink)  
Old 06-17-2004
Registered User
 

Join Date: Sep 2003
Posts: 13
Thank you Bob.

Yes, it looks like there are some files, it returned a whole bunch of results.

Is there any way i can still search by ignoring these.

i ma new to unix.

thank you
siva
Reply With Quote
  #4 (permalink)  
Old 06-18-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
The script will post the length of all lines in all files, regardless of length. To find the lines longer than 2047 chars, try

Code:
#!/bin/sh

for file in "$@"; do
  awk -v f=$file '{ if ( length( $0 ) > 2047 ) {
         printf( "%s %5d: %s\n", f, length( $0 ), $0 );
         }
       }' $file

done
and invoke it as above.

If you have a newer version of wc you can try wc -L /vob/project/* (captial L) to show the length of the longest line in each file.

So, assuming your wc supports this,
Code:
#!/bin/sh

MAX_LINE=2047
TEMPFILE=/tmp/out.$$.txt

# first, create temp file with list of files + max line lengths
find . -type f -print | xargs wc -L | grep -v total >> ${TEMPFILE}

while read term
do
   while read line
   do
      max_line=$(echo $line | awk '{print $1}')
      filename=$(echo $line | awk '{print $2}')
      if [ "$max_line" -lt "$MAX_LINE" ]; then
         echo "$filename:" >> ~/$term.txt
         grep -i "$term" $filename >> ~/$term.txt
      fi
   done < ${TEMPFILE}
done < search.strings

rm -f ${TEMPFILE}
Will do what you want, excluding any file from the search that contains a line longer than 2047 chars.

Cheers
ZB
Reply With Quote
  #5 (permalink)  
Old 06-21-2004
Registered User
 

Join Date: Sep 2003
Posts: 13
Thanks a lot Bpb.

Looks like the wc -L (-L) option is not there in this version.

just wondering how should i do.

thank you
siva
Reply With Quote
  #6 (permalink)  
Old 06-21-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Substitute
Code:
find . -type f -print | xargs wc -L | grep -v total >> ${TEMPFILE}
with

Code:
for file in $(find . -type f -print)
do
linecount=`awk 'BEGIN { f=0 } {if ( length( $0 ) > f ) { f = length( $0 ); }} END { print f}' $file`
string="$linecount $file"
echo "$string" >> ${TEMPFILE}
done
Obviously, replace the "." in the find command with the directory you are reading, /vob/project, in your case

Cheers
ZB

Last edited by zazzybob; 06-21-2004 at 03:42 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0