LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2004, 03:02 PM   #1
thatbloke
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 1
Posts: 21

Rep: Reputation: 15
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
 
Old 05-06-2004, 04:02 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 05-06-2004, 04:08 PM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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 .

Last edited by btmiller; 05-06-2004 at 04:09 PM.
 
Old 05-06-2004, 04:13 PM   #4
thatbloke
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 1
Posts: 21

Original Poster
Rep: Reputation: 15
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!
 
Old 05-06-2004, 04:31 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 05-06-2004, 04:41 PM   #6
thatbloke
LQ Newbie
 
Registered: May 2004
Distribution: Fedora Core 1
Posts: 21

Original Poster
Rep: Reputation: 15
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...
 
Old 05-06-2004, 05:15 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
executing a command at a certain time pantera Programming 6 04-05-2005 01:22 PM
Executing a command on every file in a directory asciimonster Linux - Newbie 2 06-14-2004 04:46 AM
executing from command line nodger Linux - Newbie 3 01-20-2004 11:30 AM
::Command Executing in Webmin:: XxAndyxX Linux - Software 0 03-07-2003 02:04 PM
Executing command lines in C/C++ ReverseLogic Programming 2 05-28-2002 03:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration