Posts Tagged ‘grep’

Make the most recent tex document in the current directory and open it

Wednesday, January 13th, 2016

Here’s a little bash script to compile (pdflatex, bitex, 2*pdflatex,etc.) the most recent .tex file in your current directory that contains begin{document} (i.e. the main document):

#!/bin/bash
if [ -z "$LMAKEFILE" ]; then
  echo "Error: didn't find LMAKEFILE environment variable"
  exit 1
fi
TEX=$( \
  grep -Il1dskip "begin{document}" *.tex | \
  xargs stat -f "%m %N" | \
  sort -r | \
  head -n 1 | \
  sed -e "s/^[^ ]* //")
BASE="${TEX%.*}"
if [ -z "$TEX" ]; then
  echo "Error: Didn't find begin{document} in any .tex files"
  exit 1
fi
make -f $LMAKEFILE $BASE && open $BASE.pdf

Simply use it:

texmake

Copy text from a latex file without comments newlines or comments

Monday, January 20th, 2014

Here’s a bash one-liner to copy the raw text from a LaTeX file. It strips comments (be careful if you use % in your text), removes newline, thins whitespace and finally pipes to the keyboard.

cat abstract.tex | sed -e "s/%.*$//" | tr '\n' ' ' | sed -e "s/  */ /g" | sed -e "s/^ //g" | pbcopy

If you’re issuing this from vim then you need to escape the % sign:

    cat abstract.tex | sed -e "s/\%.*$//" | tr '\n' ' ' | sed -e "s/  */ /g" | sed -e "s/^ //g" | pbcopy

List text files sorted by line count

Sunday, January 5th, 2014

Here’s a bash snippet to list the text files (only text files, no binaries) in the current directory sorted by line count:

find . -depth 1 -type f -exec grep -Il "" {} \; | xargs wc -l | sort

This outputs something like:

    6 ./alexa.h
   35 ./Astrid.h
   70 ./Henrick.sh
   74 ./dagmar.h
  133 ./Adrea.cpp
  168 ./max.cxx
  216 ./Sabina.cpp
 2339 ./Thorsten.cpp
39991 ./albrecht.off

Update: For the particular case of wc (and maybe) others its faster to do this:

wc -l `grep -Il "" *` | sort

Grab today’s Astronomy Picture of the Day and set as background

Wednesday, September 11th, 2013

I wrote this for my brother a while back. First set up your desktop to use some photo /path/to/today.jpg as a background.

Next save this in a file called apod.sh

#!/bin/bash
URL=`wget -q http://apod.nasa.gov/apod/ -O - | grep "<a href=.image" | sed -e 's/<a href=.\([^"]*\).*$/http:\/\/apod.nasa.gov\/apod\/\1/'`
wget -q "$URL" -O /path/to/today.jpg 

Note: be sure to change /path/to/today.jpg above.

Then set up a daily cronjob to execute this script:

@daily /path/to/apod.sh

Workaround for broken pdftk burst

Friday, August 23rd, 2013

The macports version of pdftk seems to be broken when using the burst command:

pdftk input.pdf burst output page-%02d.pdf

Is supposed to split apart a multipage pdf into single pages but instead produces a Java runtime exception:

Unhandled Java Exception:
java.lang.NullPointerException
   at com.lowagie.text.pdf.PdfCopy.copyIndirect(pdftk)
   at com.lowagie.text.pdf.PdfCopy.copyObject(pdftk)
   at com.lowagie.text.pdf.PdfCopy.copyDictionary(pdftk)

To get around this I wrote a short script. Save the following in a file called pdftkburst.sh:

#!/bin/bash
if [ $# -eq 0 ]
  then
    echo "Usage:"
    echo "  pdftkburst input.pdf output-%04d.pdf"
    return 1
fi

NUM_PAGES=`pdfinfo $1 | grep Pages: | sed -e "s/ *Pages: *//g"`
#echo "NUM_PAGES: $NUM_PAGES."
for i in $(seq 1 ${NUM_PAGES})
do
  #echo "printf \"$2\" $i"
  PAGE_NAME=`printf "$2" $i`
  pdftk $1 cat $i output $PAGE_NAME
  #echo "Creating $PAGE_NAME"
done

Then you can burst your pdfs using:

./pdftkburst.sh input.pdf page-%02d.pdf

Which indeed creates page-01.pdf, pages-02.pdf etc.

Determine Eigen version with grep

Friday, August 16th, 2013

Here’s a small bash one-liner to determine the version of Eigen you have install with macports:


grep "#define EIGEN_[^_]*_VERSION" /usr/local/include/eigen3/Eigen/src/Core/util/Macros.h

Issuing this you should see something like:


#define EIGEN_WORLD_VERSION 3
#define EIGEN_MAJOR_VERSION 1
#define EIGEN_MINOR_VERSION 2

which would mean you’re using version 3.1.2.

Slightly improved colored cal output

Monday, July 29th, 2013

I found a nice alias to color the output of the cal function. But on the 20th of July this produced:

     July <span style="color:red;font-weight:bold;">20</span>13
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 <span style="color:red;font-weight:bold;">20</span>
21 22 23 24 25 26 27
28 29 30 31

I refined the grep search a bit with this

alias cal='cal | grep --color=auto -E "( |^)$(date +%e)([^0-9]|$)|$"'

which produces the correct result:

     July 2013
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 <span style="color:red;font-weight:bold;">20</span>
21 22 23 24 25 26 27
28 29 30 31

Update: The alias above ruins the ability to pass arguments to cal. Here’s a slightly improved version:

cal() { /usr/bin/cal "$@" | grep --color=auto -E "( |^)$(date +%e)([^0-9]|$)|$";}

Unfortunately it’ll still color today’s day of the week even if the month or year passed in the cal arguments are not the current month or year.

ACRNYM: Title of research paper follows in SIGGRAPH paper titles

Thursday, September 22nd, 2011

Recently I posted a list of over 4000 SIGGRAPH technical papers titles. You can see some interesting patterns browsing this list. Since the very beginning of SIGGRAPH (see “Modelaide: a computer graphics program for the evaluation of mathematical models”) there has been a long tradition of papers with titles beginning with a pet name for their algorithm or software. Like recently “MeshFlow: interactive visualization of mesh construction sequences”. With my list its easy to suck out all the paper titles that
begin with “SomeWord:” and look at the names people are using.

MeshFlow
LR
HOT
OverCoat
Spark
GlobFit
ShadowDraw
CATRA
MovieReshape
envyLight
PantaRay
RepFinder
OptiX
VideoMocap
Popup
NETRA
Dinus
GradientShop
DiagSplit
RenderAnts
Sketch2Photo
Bokode
SubEdit
iWIRES
PatchMatch
SkyFinder
FlexiStickers
GRAMPS
VirtualStudio2Go
IGT
AppProp
BSGP
Larrabee
SOHO
Prakash
AppWand
ShapePalettes
VideoTrace
PCU
Ldr2Hdr
TRACKS
SIMBICON
Plushie
FastLSM
FiberMesh
Videogames
SmoothSketch
Glift
SCAPE
MoXi
GoLD
Lightcuts
Geopostors
Color2Gray
RPU
Blister
Lpics
DART
CrossY
VisionWand
GADGET
VisualIDs
TensorTextures
Textureshop
DISCO
SwingWrapper
iLamps
Cg
TreeJuxtaposer
Twister
Matchmaker
VisBio
CHARMS
Chromium
DyRT
BEAT
Kizamu
WordsEye
WireGL
DAB
FoleyAutomatic
Pomegranate
Surfels
QSplat
Deepwave
Hypercosm
la_fabrique
IceBorg
Alice
SimsalaGrimm
Rivet
Teddy
Skin
Feline
LCIS
ArtDefo
VisAD
NeuroAnimator
mediaBlocks
MAPS
Constellation
Wires
InfiniteReality
SCAAT
Alice
NICE
Osmose
VENUS
SKETCH
Improv
Talisman
uisGL
OBBTree
VRML
TicTacToon
AutoKey
Corrigendum
Cantata
Euphrates
Yaged
TBAG
FBRAM
Corrigenda
Placeholder
Pad
EXACT
Leo
VIEW
Radioptimization
PixelFlow
CONDOR
Editorial
Sculpting
Inkwell
Metamouse
ConMan
FRAMES
GRAPE
Corrigendum
MIKE
Rooms
Squeak
SYNGRAPH
INCENSE
MAPQUERY
PICTUREBALM
BUMPS
QUADRIL
ATLAS
ATOMLLL
GAIN
TIGS
CASS
GPGS
Glide
ECOSITE
SIGCHR
ARTES
PRIMS
BGRAF2
Typer
DDA
ARTSPEAK
SUGAR
Grapheasy
WHATSISFACE
IMAGE
Wave
CARTE
TREE
XPLG
DRAW
APLBAGS
MAPS
KARMA
Modelaide

Set up virtual host on mac os x

Tuesday, September 6th, 2011

We’re using SVN to version control our group’s webpage which is all php scripts. If everything were html files then making edits on a locally checked out copy would be easy since I can just open the files in a browser and check my edits. But, being PHP scripts, to properly see the sites I need a php server running which can host the local copy of the site and I can see the edits I make live. Then finally when I’m sure everything looks and executes correctly I can check in the edited version and update the version on the real server. Here’s how I constructed my local setup.

Check out svn to some directory, from now on referred to as “path/to/htdocs-igl”:


svn co https://private.svn.server.com/trunk/htdocs-igl path/to/htdocs-igl

Be sure that no apache servers are not running. The following in order should to do that:


sudo apachectl stop

Use ps and grep to determine if they really stopped. This should return nothing:


ps -ef | grep httpd | grep -v grep

If it returns some lines, then kill each replacing [PID] with each row’s process ID:


sudo kill [PID]

If the process didn’t really die then try:


sudo kill -9 [PID]

And after you think you’ve done enough killing, issuing this again for good measure:


sudo apachectl stop

Now that we’re sure no apache servers are running. Start one:


sudo apachectl start

To check that it’s working browse to http://localhost/. You should see something fully load other than “Not found, blah blah”.

Now, add the following as a new line at the bottom of the file /etc/hosts:


127.0.0.1       localigl

Now you should be able to go to http://localigl/ and see the same thing as you did with http://localhost/.

To have http://localigl/ point to your checked out version of the website, you need to edit the file /etc/apache2/users/[your mac username].conf:


NameVirtualHost *:80

<Directory "/Users/[your mac username]/Sites/">
    Options Indexes MultiViews Includes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /Users/[your mac username]/Sites/
</VirtualHost>

<Directory "/absolute/path/to/htdocs-igl/">
    Options Indexes MultiViews Includes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    ServerName localigl
    DocumentRoot /absolute/path/to/htdocs-igl
</VirtualHost>

Replacing, in the file name and the file contents, [your mac username] with, uh, your mac username.

You must give execute access to parent directory and the root directory of the checked out website:


chmod 755 path/to/htdocs-igl/..
chmod 755 path/to/htdocs-igl

Finally, restart the server with:


sudo apachectl graceful

If you now go to http://localigl/ you should see a listing of the checked out website’s root directory contents (or if you’re using index.html, you’ll see that rendered file). Since we’re using PHP for everything we must enable php scripts.

Uncomment the following line in /etc/apache2/httpd.conf to look like this:


LoadModule php5_module        libexec/apache2/libphp5.so

And again restart the server:


sudo apachectl graceful

Now when you go to http://localigl/ you should see your php scripts executed and rendered.

Source This document is well written but did not enough stress that you must stop all servers (that you may have accidentally started when fooling around) before starting the steps. And it’s nice to repeat his steps with exact names closer to my current set up.

svn get last commit message

Wednesday, August 31st, 2011

Here’s a quick bash one-liner to get the last svn commit message:


svn log -q -v --xml --with-all-revprops -r committed | grep msg | sed -e "s/<msg>\([^<]*\)<\/msg>/\1/g"


Update:
Not sure what the difference between “committed” and “head” is, but now “head” is giving me the correct message:


svn log -q -v --xml --with-all-revprops -r head | grep msg | sed -e "s/<msg>\([^<]*\)<\/msg>/\1/g"