running a looping script for all files in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting running a looping script for all files in directory
# 1  
Old 09-04-2008
running a looping script for all files in directory

I have three different linux command scripts that I run for 20+ files in one directory.

it goes like this
FIRST SCRIPT:
grep 'something' -w file > newfile1
.
.
.
grep 'something -w file > newfile20

then I take all these 'newfileN' and run this:
awk 'BEGIN { format="%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" } { printf(format,$1,$15,$4,$4,$4,$4,$2,$3,$4,$4)}' newfile1 > new.newfile1
.
.
.
awk 'BEGIN { format="%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" } { printf(format,$1,$15,$4,$4,$4,$4,$2,$3,$4,$4)}' newfile20 > new.newfile20

then finally I take these 'new.newfileN' and do this:
sed -e 's/[+-]/U0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/+/F/1' -e 's/-/R/2' -e 's/[+-]/../2' -e 's/chr//1' new.newfile1 > new.new.newfile1
.
.
.
sed -e 's/[+-]/U0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/+/F/1' -e 's/-/R/2' -e 's/[+-]/../2' -e 's/chr//1' new.newfile20 > new.new.newfile20


So, is there a way I can make this all into one script so instead of copying and pasting each scripted line 20+ times.

thanks
# 2  
Old 09-05-2008
Try this perhaps (untested):

Code:
for f in $(seq -f file%g 1 20)
do
    grep 'something' -w $f > new$f
    awk -v OFS='\t' '{ print $1,$15,$4,$4,$4,$4,$2,$3,$4,$4 }' new$f > new.new$f
    sed -e 's/[+-]/U0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/+/F/1' -e 's/-/R/2' -e 's/[+-]/../2' -e 's/chr//1' new.new$f > new.new.new$f
done

# 3  
Old 09-05-2008
If you don't need all of the temporary files in between:

Code:
for f in $(seq -f file%g 1 20)
do
    grep 'something' -w $f |
    awk -v OFS='\t' '{ print $1,$15,$4,$4,$4,$4,$2,$3,$4,$4 }' |
    sed -e 's/[+-]/U0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/[+-]/0/2' -e 's/+/F/1' -e 's/-/R/2' -e 's/[+-]/../2' -e 's/chr//1' > new.$f
done

# 4  
Old 09-05-2008
Thank you, I haven't tried but I have question before I do. Sorry if this is an obvious question, but how do I execute this? Do I copy it into a file and then call it from the command line?
# 5  
Old 09-05-2008
Okay, I think I figured that out. i created a file.sh (with the proper heading and chmod it to 755), then ran it
> sh file.sh

then I got a return error

seq: command not found

looks like my mac doesn't have seq. can i use jot? if so, how would i set it up?

thanks a bunch!
# 6  
Old 09-05-2008
Why did you say "linux command scripts" if you are using a Mac??

Change the first line to for f in file1 file2 file3 ... up to file20 and it should work. Or else use a counter...

You shouldn't need to run it with sh. Once you have made it executable, either use ./file.sh (if you are in the same directory), or use the full path, e.g. /some/directory/file.sh.
# 7  
Old 09-05-2008
Sorry about the confusion. I'm still trying to get a handle on all this. I'm connected to my mac at work via ssh (using my PC from home).

I did what you suggested and it worked like a charm.

but is there a way to forgo writing 20 file names in the first line and instead call them all like you initially had it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running script in crontab in a specific directory

I am trying to run a script from crontab but the entire script (which is 70+ lines) is written in bash and I need it to run from a certain directory. So when I run it manually I cd to /local/mnt/scr and then type ./reordersc and it works fine. However, I want it to run from the crontab and I... (20 Replies)
Discussion started by: newbie2010
20 Replies

2. Shell Programming and Scripting

Looping to check the currently running process.

Hi Gurus, t=`ps -u irb|grep -v grep|grep BSNL_PAYMENT_C` echo $t if then echo $? echo "Process Creation is Running ...." else echo "Process went down at $dat $tim" fi How would i use loop so that the script continously checks for the current status of this process.... (1 Reply)
Discussion started by: ankitknit
1 Replies

3. Shell Programming and Scripting

Running shell script from any directory

Greetings all, We have an older environment that we are trying to bring up to par with our current Production and Test servers. We have a command on our production boxes, UniqueScriptName.sh, and we can issue that command from any directory and it works. On our older environment, our... (1 Reply)
Discussion started by: jeffs42885
1 Replies

4. Shell Programming and Scripting

ksh script not running in another directory on same server

I have a script that runs fine under my home directory. No syntax errors, runs and returns as expected. When I migrate the script to the UAT directories on the same server for User testing, I get a syntax error. I've checked to make sure the .profile I'm using is the same in the UAT... (1 Reply)
Discussion started by: mpflug
1 Replies

5. Shell Programming and Scripting

Rename folder/directory after running sort script

Hello, I'm trying to create a script that sorts documents by author (found in file name) and then create a directory for that author. For example, Input: John - Paper_1.txt John - Paper_2.txt Mark - Paper_1.txt Jill - Paper_1.txt Output: dir/John/Paper_1.txt dir/John/Paper_2.txt... (1 Reply)
Discussion started by: jl487
1 Replies

6. Shell Programming and Scripting

Help Looping through files in Vi Script

I am trying to write a script that loops through all the files in the current directory that end in '.slg.gz' and runs a parser on each file. Here is my code: #!/bin/bash FILES_HOME = 'dirname $0' for i in $(ls $FILES_HOME/.slg.gz$);do ./run-feature-parser $(i) > OUTPUT.csv done ... (1 Reply)
Discussion started by: kssteig
1 Replies

7. Shell Programming and Scripting

merge two files via looping script

Hi all, I hope you can help me. I got a file a and a file b File a contains a b c d e f g h File b contains 1 2 3 (8 Replies)
Discussion started by: stinkefisch
8 Replies

8. Solaris

running script in any directory

hi im a cobol programmer but im new in unix. I just want to know how can i run script even if it is in other directory?? Thanks, (1 Reply)
Discussion started by: shinjeck
1 Replies

9. Shell Programming and Scripting

How to know the directory of the file containing the running script?

When you run a script, how will you know the directory for the actual file that contains the script. The code should be like .Me in VB. I dont know how we do it in unix. Is it possible? (8 Replies)
Discussion started by: alestoquia
8 Replies

10. Shell Programming and Scripting

rm files in a directory, looping, counting, then exit

I am trying to write a script that will look for a file in a directory, then remove it. I need it to loop until it has removed a certain number of files. Is it better to do a repeat or to list each file in a pattern? Files will be numbered like RAF.01.*, RAF.02.*, etc. Thanks, James (6 Replies)
Discussion started by: JporterFDX
6 Replies
Login or Register to Ask a Question