The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-04-2008
labrazil labrazil is offline
Registered User
  
 

Join Date: Sep 2008
Location: San Diego, CA
Posts: 60
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 (permalink)  
Old 09-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 09-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 09-05-2008
labrazil labrazil is offline
Registered User
  
 

Join Date: Sep 2008
Location: San Diego, CA
Posts: 60
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 (permalink)  
Old 09-05-2008
labrazil labrazil is offline
Registered User
  
 

Join Date: Sep 2008
Location: San Diego, CA
Posts: 60
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 (permalink)  
Old 09-05-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 09-05-2008
labrazil labrazil is offline
Registered User
  
 

Join Date: Sep 2008
Location: San Diego, CA
Posts: 60
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?
Closed Thread

Bookmarks

Tags
awk, dos2unix, for-while loops, tr

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0