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.
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?
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.
The "steps to do" was meant as a "fill in the blank" comment, I guess.
The use of ls -l if you don't actually use the long format for anything is obviously Useless. Fixing that also removes the need to remove the "total" line from the sum.
But anyway, if you don't use the count for anything, either, just loop over the file names.
for file in *; do
# stuff
done
Tangentially, I guess RahulJoshi really meant -ge rather than -lt
Okay, so is the error I'm getting in post #10 above because of a formatting issue? I'm not sure I understand what is going on. It was working before then it's not???
I'm not sure either... is that all of your code that you have posted in #10, or is there more in the script? Are the filenames exactly as you have posted (i.e. file1 file2 file2?
yes, that's the exact code, nothing more. I even tried changing the quotes from ' to `. Same error. And all the files are right in the same directory...
I tried using the shebang line but it didn't work. it stated:
-bash: ./loop.sh: /usr/bin/ksh^M: bad interpreter: No such file or directory
I created a new sh script with this
echo "Hi there.";
and after I chmod to 755 and typed ./file.sh it worked with no error. this is telling me that there's a format issue with the original one, but I can't find it....
The ^M holds the key... you must be creating these scripts under Windows and uploading them to Unix, right? Windows and Unix use different text file formats. Windows ends lines with Carriage Return and Line Feed characters (CR/LF), whereas Unix just uses a Line Feed (LF). You need to convert the script file(s) to Unix format using dos2unix windowsfile > unixfile or tr -d '\r' < windowsfile > unixfile.
You may find life simpler if you learn how to use a Unix editor and edit them directly on the system where you are running them.