Reputation: 10689
I'm pretty unfamiliar with awk
, but I am looking for a way to iterate through all files in a directory and execute a simple awk
command. My command is a simple substitution that replaces all tabs with two spaces.
awk '{gsub("\t"," ");print}'
How can this be extended to loop through a directory and execute the command on all files?
Upvotes: 3
Views: 10022
Reputation: 80384
Pass the files to awk on the command line of course:
$ awk 'program' *
But probably it is easier to use
$ perl -pe 's/\t/ /g' *
Or, if you would rather have an in place edit, simply:
$ perl -i.orig -pe 's/\t/ /g' *
Upvotes: 3
Reputation: 1
A useful snippet, that shows awk processing a list of files as if it is one stream (notice how the line number is used)
find $HOME/.ssh -name "*.pub" -type f \
-exec awk '{printf "%c) %12s %s...%s %s\n", 64 + NR, $1, substr($2,1,7), substr($2,length($2)-7), $3}' \
{} +
example output
A) ssh-dss AAAAB3N...WhdJHA== [email protected]
B) ssh-ed25519 AAAAC3N...9a5kCpbT [email protected]
C) ssh-rsa AAAAB3N...fzh3oUGZ [email protected]
Upvotes: 0
Reputation: 11
an improved version of Heisenbug's idea
find . -type f -exec awk '{gsub("\t"," ");print}' {} \;
it avoids calling another program like xargs. It seems to be a bit faster with some tests I did.
Upvotes: 1
Reputation: 342363
you can do it with one awk
command
awk -F"\t" '{$1=$1;print $0 >FILENAME}' OFS=" " file
Upvotes: 0
Reputation: 39164
use find and redirect the output to awk:
find . -type f | xargs awk '{gsub("\t"," ");print}'
Upvotes: 0