The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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
Read the directory path sollins Shell Programming and Scripting 1 11-05-2008 05:09 AM
read files from directory Satyak Shell Programming and Scripting 2 10-17-2008 05:20 AM
Read Files from a Directory kiran_418 UNIX for Dummies Questions & Answers 2 04-18-2008 01:23 PM
Read from fileList.txt, copy files from directory tree fxvisions Shell Programming and Scripting 1 10-30-2007 05:17 PM
FTP to read a directory jikyj Shell Programming and Scripting 11 08-27-2003 11:23 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-19-2009
flevongo flevongo is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 3
Using Awk within awk to read all files in directory

I am wondering if anyone has any idea how to use an awk within awk to read files and find a match which adds to count.
Say I am searching how many times the word crap appears in each files within a directory. How would i do that from the command prompt ...

thanks
  #2 (permalink)  
Old 09-19-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,396
awk can do it..
Code:
awk '/crap/{crap++}END{print crap}' filename
  #3 (permalink)  
Old 09-19-2009
flevongo flevongo is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 3
thanks vidyadhar85...
that works for one file. So I would have to do that for every file in the dirctory. I was hoping there was a way to do that for all the files in the directory with a single command from the prompt.
  #4 (permalink)  
Old 09-19-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,307
This should be sufficient, check the man page of grep.:

Code:
grep -c 'crap' *
  #5 (permalink)  
Old 09-19-2009
flevongo flevongo is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 3
Thanks Franklin52....
this was the command I used
ls -l | awk 'BEGIN {crap=0} {if (^[^d]) {awk '/crap/{crap++}' $8} END {print crap}'
I was hoping to be able to just print the total count ...
but kept getting a syntax error
  #6 (permalink)  
Old 09-19-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,396
how can u use awk in side awk.. you can't do so...
so u want total count of crap in a dir??
if you want the count no of crap from a dir use
Code:
 
awk '/crap/{crap[FILENAME]++}END{for(crapcount in A){print A[crapcount]" "crapcount}}'  *

Last edited by vidyadhar85; 09-19-2009 at 04:16 PM..
  #7 (permalink)  
Old 09-19-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 651
These solutions count the number of lines containing the character sequence "crap". However, the OP was looking for the number of word occurrences per file. I think what is needed is something like this:

Code:
 for i in *; do printf "$i: "; grep -c "\bcrap\b" <(xargs -n1<$i); done

Last edited by Scrutinizer; 09-19-2009 at 05:29 PM..
Reply

Bookmarks

Tags
awk, awk within awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0