Run a script over multiple files in unix
Asked Answered
Z

8

8

Started to learn some shell scripting. I have a perl script that runs on one file. How would I write a shell script to run the perl script a bunch of times on all files with keyword "filename" in it?

So in English,

for /filename/ in filenames  
    perl myscript.pl completefilename

Thanks.

Zacharie answered 6/7, 2009 at 16:10 Comment(0)
I
12
find . -name "filename*" -exec perl myscript.pl '{}' \; 
Impostor answered 6/7, 2009 at 16:17 Comment(0)
A
4
for i in $(\ls -d filenames)
do
    perl myscript.pl $i
done

The backslash in front of the 'ls' command is to temporarily disable any aliases.

HTH

Ancon answered 6/7, 2009 at 16:14 Comment(0)
B
4
find . -path "\*filename\*" -exec perl myscript.pl {} \;

edit: escaped stars, didn't want the markup here

Boarhound answered 6/7, 2009 at 16:14 Comment(0)
M
1

In bash:

files=`ls -1 *`
for $file in $files;
do
    perl myscript.pl $file;
done
Mailbag answered 6/7, 2009 at 16:13 Comment(1)
This only works as long as there are no filenames with spaces in them.Chavarria
G
1

One liner:

$ for file in filename1 filename2 filename3; do perl myscript $file; done

Instead of the space separated list of filenames you can also use wildcards, for instance:

$ for file in *.txt *.csv; do perl myscript $file; done

Gariepy answered 6/7, 2009 at 16:14 Comment(0)
W
1

And if you have spaces in your filenames, use the old standby

find . -maxdepth 1 -print0 | xargs -0 perl myscript.pl
Weitzman answered 6/7, 2009 at 16:15 Comment(0)
A
0

FILES="keyword"

for f in "$FILES" do perl myscript.pl $f done

From http://www.cyberciti.biz/faq/bash-loop-over-file/

Afar answered 6/7, 2009 at 16:13 Comment(1)
But $f pulls up the keyword, whereas I would like the entire filename (that includes the keyword).Zacharie
D
0

I personally use the zsh shell, which gives you a very nice way to run a command recursively on a set of subdirectories. It also allows you to change the suffix of the file, which is handly when using lame to create MP3 files of .wav files:

for i in **/*.wav; lame $i $i:r.mp3

You can also pipe the output of one command to another, which I something I use frequently when I'm downloading a number of bittorrent files and want to see the percentage that each download has completed:

for i in **/*.txt; grep -H percent $i | tail -1

Dentelle answered 6/7, 2009 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.