LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   executing a command as part of printf... (https://www.linuxquestions.org/questions/linux-newbie-8/executing-a-command-as-part-of-printf-178639/)

thatbloke 05-06-2004 02:02 PM

executing a command as part of printf...
 
hi, basically i want to be able to do a search using find inside a script which takes a path as an argument and searches for all files with .htm in the extension, but then use printf on each file (with the -exec option of find) to print out the name of the file, then use wc to print out the number of lines, number of words and number of chars in the file... here's what i have so far: i want the calls to wc to be interpreted by the shell and run so that they provide the integer, but it just prints out what i have put in there and warns me that they arent integers... any ideas?

find $1 -name '*.*htm*' -exec printf "Webpage: %s, Number of lines: %d, Number of Words: %d, Number of Characters: %d" {} 'wc -l {}' 'wc -w {}' 'wc -c {}' \;

$1 is the pathname sent into the script. How do I get the wc parts to return the integers that they are supposed to?

thanks

Tinkster 05-06-2004 03:02 PM

That works for me ... slightly different approach, though.

Quote:

printf.sh
Code:

#!/bin/bash
lines=`wc -l $1 | awk '{print $1}' `
word=`wc -w $1 | awk '{print $1}' `
chars=`wc -c $1 | awk '{print $1}' `
echo "Webpage: $1 - Number of lines: $lines, Number of Words: $word, Number of Characters: $chars"


Code:

find -iname "*.htm*" -exec printf.sh {} \;
I've never used printf before (actually, I didn't even
know it exists ;})

The problem you're facing was that wc, along with the actual
count, will also pass back the file's name, which is what my
awk statement took care of.

HIH!


Cheers,
Tink

btmiller 05-06-2004 03:08 PM

Try enclosing the wc commands in backticks (`). The one problem I see with this is that the wc output also prints out the file -- I think you can get around this with `cat {} | wc -l` (seems to work for me at any rate. Hopefully someone better at shell programming will have a more elegant solution, but this is what I came up with.

[edit]

Tinkster beat me to a better solution -- I really have to learn awk one of these days :) .

thatbloke 05-06-2004 03:13 PM

Hi, that has kind of solved my problem... i was using single quotes rather than the backticks! (doh!) problem now is that wc tells me that the file does not exist, although the {} should specify the currently being processed file by the find command... so help! :D

Tinkster 05-06-2004 03:31 PM

Quote:

Originally posted by btmiller
Tinkster beat me to a better solution -- I really have to learn awk one of these days :) .
awk is quite handy, I should be using it more than
I actually am :}


Cheers,
Tink

thatbloke 05-06-2004 03:41 PM

heh... thing is, this is an assignment i have for my degree (i just started doing this stuff) and i need to use a shell script to do it... though using awk may not be the worst thing in the world... :) I do kinda know awk and am actually calling an awk script i wrote in this script in a different place... :)

Tinkster 05-06-2004 04:15 PM

In this case you may want to use that mini-script
as a function? :)



Cheers,
Tink


All times are GMT -5. The time now is 06:35 AM.