Read in a list of files and loop over them in bash

Needed to do this and after a bit of looking around (Bash scripting) came up with this:


#!/bin/bash

FILE_LIST_SOURCE=listoffiles.txt
DEFAULT_FILE="some.default.file"

[ -f $FILE_LIST_SOURCE ] && LIST_OF_FILES=$(cat $FILE_LIST_SOURCE)

LIST_OF_FILES=${LIST_OF_FILES:-$DEFAULT_FILE}

#echo -e "\\nLIST_OF_FILES:\\n$LIST_OF_FILES"

echo "List of files:"
for i in $LIST_OF_FILES; do
    echo $i
done


Share

Python!

Wanted to find all files which contained a specific string and then extract a part of the line we were interested in.

First the GREP:

grep -R --include=*.vm  * > results.txt

then run this python script:

import re
for line in open('editablecomponents.txt'):
	match = re.search(r'#editableComponent\("(.*?)"', line)
	if match:
		print match.group(1) + "Display.vm"
Share

Sweet Search And Highlight Command

Been doing quite a bit of manipulating LaTeX files on the command line recently, and have a very sweet little command for searching for text in multiple files:

grep -r Hoffman */*.tex | less +/Hoffman

Where “Hoffman” is the search term which can of course be any regular expression.

The “less” command has a very convenient argument “+” which allows you to pass a search term in and will automatically highlight it for you.

The -r in grep recurses directories and */*.tex looks for all tex files. the | pipes the result to less.

sweet.

Share