#How to write a bash for loop #Written by E Heppenheimer, Fall 2017 #For loops are awesome and can save you a lot of time! #the basic idea is that you would like to repeat the same set of commands on many files #the general syntax is: for BLANK in FILES do SOMECOMMAND done #walking through the simplest example: #first specify the files of interest #you can do this by listing all the file names $ files="sample1 sample2 sample3" #but that becomes really inefficient for more than a handful of files #it is usually easier to use a wildcard character (*) #for example, if all the file names start with "sample": $ files=sample* #or if all the files end with ".txt" $ files= *.txt #and so on... #once you have your files specified, open the loop: $ for f in $files #note the "f" could be any letter, "i" and "j" are also popular choices do #then specify the set of commands. Here is a simple cat command, but it could be anything cat $f > outputfile.txt #then you close the loop with the word "done" done #Now some more complicated things: #Extracting the file name prefix: #this is possibly the hardest part of for loops #in short, you want to generate one output file for every input file, and keep the same prefix of the file name #This can be done with awk #assuming everything before the first period is the prefix (e.g. Important_Data_File_1.txt, prefix would be "Important_Data_File_1") pre=$(echo $f |awk -F'.' '{print $1 }') #you may also want to specify a suffix for the output file #for example, if your output is a sorted text file: suf="_sorted.txt" #putting this all together #this is a for loop to sort a set of files #maintains the original file prefix #suffix indicates the file is sorted files=*.txt for f in $files do pre=$(echo $f |awk -F'.' '{print $1 }') suf="_sorted.txt" sort -n $f > $pre$suf done