![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| looping thru filenames in a directory | silas.john | Shell Programming and Scripting | 1 | 07-02-2008 09:27 AM |
| running script in any directory | shinjeck | SUN Solaris | 1 | 10-26-2007 06:23 AM |
| Looping through the directory | mahalakshmi | Shell Programming and Scripting | 6 | 11-23-2006 01:23 AM |
| How to know the directory of the file containing the running script? | alestoquia | Shell Programming and Scripting | 8 | 03-24-2006 06:36 PM |
| rm files in a directory, looping, counting, then exit | JporterFDX | Shell Programming and Scripting | 6 | 07-18-2002 09:56 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
|
|
||||
|
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
|
|
||||
|
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! |
|
||||
|
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. |
|
||||
|
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? |
![]() |
| Bookmarks |
| Tags |
| awk, dos2unix, for-while loops, tr |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|