Using Awk within awk to read all files in directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using Awk within awk to read all files in directory
# 1  
Old 09-19-2009
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  
Old 09-19-2009
awk can do it..
Code:
awk '/crap/{crap++}END{print crap}' filename

# 3  
Old 09-19-2009
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  
Old 09-19-2009
This should be sufficient, check the man page of grep.:

Code:
grep -c 'crap' *

# 5  
Old 09-19-2009
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  
Old 09-19-2009
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 05:16 PM..
# 7  
Old 09-19-2009
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 06:29 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk separate files to one directory

I am trying to output all files that are made by this awk to a specific directory. awk -F '' '{f = $3 ".txt"; print > f}' input.txt Since the actual data has several hundred files I redirect the output (well tried to) to a directory. awk -F '' '{f = $3 ".txt"; close($3 ".txt")} print >... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Iterating awk over a directory of files?

I need to run the same awk function over an entire directly of files. This is the awk: awk '{$(NF+1)=1}1' Is there a way that I can run this command once over all of the files, along the lines of: awk '{$(NF+1)=1}1' * so that I do not have to run this several times? My main concern is... (2 Replies)
Discussion started by: owwow14
2 Replies

3. Shell Programming and Scripting

Splitting Files with awk into other directory

I am trying to split into different files using awk: cat files | gawk '$1 ~ /---/ || $1 ~ /^deleting$/ || $1 ~ /^sorting$/ || $1 ~ /==/ {print}'| gawk '$1 ~ /---/ || $1 ~ /^deleting$/ || $1 ~ /^sorting$/ || $1 ~ /==/ {print}' |gawk '//{x="F"++i;}{print > x;}' What I am trying to do is make F*... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. Shell Programming and Scripting

Using awk on multiple files in a directory

so i have a file system: /data/projects in this file system, there's about 300 files. on all files in this directory, i'm running: egrep -r 'Customer.*Processed' /data/projects/* is there an efficient (fast) awk way of searching through each file in the directory and providing an... (9 Replies)
Discussion started by: SkySmart
9 Replies

5. UNIX for Dummies Questions & Answers

awk - how to read multiple files

Hi, is there a ways to read multiple files in a single awk command? For example: awk -f awk_script file1 file2 file3 I've google it, most of them suggest using FNR. But I don't understand how it works. It will be a great help if someone able to explain it in simple term with some example. (4 Replies)
Discussion started by: KCApple
4 Replies

6. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

7. UNIX for Advanced & Expert Users

Use awk to read multiple files twice

Hi folks I have a situation where I am trying to use awk to compute mean and standard deviation for a variable that spans across multiple files. The layout of each file is same and arranged in 3 columns and uses comma as a delimiter. File1 layout: col1,col2,col3 0,0-1,0.2345... (13 Replies)
Discussion started by: scandy
13 Replies

8. Shell Programming and Scripting

Apply 'awk' to all files in a directory or individual files from a command line

Hi All, I am using the awk command to replace ',' by '\t' (tabs) in a csv file. I would like to apply this to all .csv files in a directory and create .txt files with the tabs. How would I do this in a script? I have the following script called "csvtabs": awk 'BEGIN { FS... (4 Replies)
Discussion started by: ScKaSx
4 Replies

9. UNIX for Dummies Questions & Answers

Writing awk script to read csv files and split them

Hi Here is my script that calls my awk script #!/bin/bash set -x dir="/var/local/dsx/csv" testfile="$testfile" while getopts " f: " option do case $option in f ) testfile="$OPTARG";; esac; done ./scriptFile --testfile=$testfile >> $dir/$testfile.csv It calls my awk... (1 Reply)
Discussion started by: ladyAnne
1 Replies

10. Shell Programming and Scripting

awk read input files

hi, i'm a beginner in writing awk scripts and I have a problem with reading input files. Requirement for my programm: compare file1 to file2 and check if value in column1 is equal and value in column5 is different. File 1: 180 P 01.01.2008 30.06.2008 2 180 P 01.07.2008 ... (10 Replies)
Discussion started by: tgooper
10 Replies
Login or Register to Ask a Question