Grep your omnioutliner files


#1

I came up with a way to do it, with a few issues I’ll discuss at bottom. This works, and I’m using it now.

how to grep your omnioutliner ooutline files

#first move .ooutline files to another directory

mkdir -p outlines2
for file in *.ooutline; do
echo cp ‘"$file"’ ./outlines2/
done

#then in the remove the .ooutline extension from the files

cd ./outlines2
for file in *.ooutline; do
echo mv ‘"$file"’ $(echo $(echo ‘"$file" | cut -f 1 -d ‘.’ )’) ;
done

#then rename the files to .zip

for file in * ; do
echo mv ‘"$file"’ $(echo $(echo ‘"$file".zip )’) ;
done

#then unzip the outline file contents into separate folders

for file in *.zip ; do
echo unzip ‘"$file"’ -d $(echo $(echo ‘"$file" | cut -f 1 -d ‘.’ )’) ;
done

#now you can grep your outline files for content:

find . -iname *.xml -print0 | xargs -0 grep -Eo ‘.{0,40}Peter.{0,40}’ *.xml


Issues:

  1. I have to run each for loop as a script and then pipe the generated mv etc commands to an output file, which I then chmod a+x and run as a batch file. Why? Because the commands in the loop, while generate fine with the echo, don’t run; and when I remove the echo, I get syntax errors.

  2. This is my first ever real bash script, so, you know, it is what it is. I’ll get better.

  3. the escaped single quotes make it work with filenames with spaces, which I have many of.

  4. the rename to zip loop also renames my sh scripts grrr oh well.


that said, this does exactly what I want it to do. I get output like this from the grep: (excuse content, it’s for a novel)


#2

By the way, Omnigroup, you might tell people they can just use FoxTrot Professional. It’s 120 bucks but well worth it.

c87e703e7e08e52ae46345ac2a5e5f1cdeee4e88_1_690x376


#3

I am so grateful that you posted this. I don’t need this exact problem solved. But, it’s very encouraging when someone struggles with solving a problem and then is ingenious enough to come up with a method like this. I have a few of my own automation problems to get back to now! Thanks for posting.


#4

here’s the finished version, which runs as a script with a parameter, so:

./oo2sub.sh SubfolderName

sets up your search folder, then you can grep as before.

cd SubfolderName
find . -iname *.xml -print0 | xargs -0 grep -Eo ‘.{0,40}Peter.{0,40}’ *.xml


#!/bin/bash

echo “a new folder: $1”

mkdir -p “$1”
for file in *.ooutline; do

cp  "$file" ./"$1"/

done

cd “$1” ;
pwd;

for file in *.ooutline; do

mv “$file” “$(echo $file | cut -d’.’ -f 1)”

done

for file in * ; do

mv “$file” “$file”.zip

done

for file in *.zip ; do

unzip “$file” -d “$(echo $file | cut -d’.’ -f 1)”

done

=====

and here’s a simpler script, the one I’m actually using. labels each line with the filename. Note I needed both -H and --label switches to make that work.

note that -m 10 limits matches per file to 10, but this can be removed if you want the whole smash.

@kcase this is my favorite :-)

#!/bin/bash

for file in *.ooutline; do
if ( unzip -c “$file” | grep --label="$file" -H -m 10 -Eo “.{0,20}$1.{0,20}” ) ; then
printf “\n\n”
fi
done

Note, could probably get rid of the bash loop, and just use find, and make this work with something like find | unzip | grep.

that first script can probably be made simpler…this does the same thing:

#!/bin/bash

echo “a new folder: $1”

mkdir -p “$1”
for file in *.ooutline; do

cp  "$file" ./"$1"/

done

cd “$1” ;
pwd;

for file in *.ooutline ; do

unzip “$file” -d “$(echo $file | cut -d’.’ -f 1)”

done

find . -iname *.xml -print0 | xargs -0 grep -Eo ‘.{0,40}Peter.{0,40}’ *.xml

however I’ve since discovered that this last script and first script don’t handle filenames with spaces in them–the unzipped folders get created but the find | grep isn’t returning results. However, the two scripts in the middle do. I’d stick with them.