Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Monday, December 29, 2014

Bash Script to Cleanup File

This bash script will do three things: 1. Remove Carriage Return (\r) 2. Replace tab character with four spaces (configure as you want) 3. Add a trailing line feed to the file

#!/bin/bash

platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Darwin' ]]; then
   platform='MAC'
elif [[ "$unamestr" == *CYGWIN* ]]; then
   platform='WIN'
elif [[ "$unamestr" == "Linux" ]]; then
   platform='LINUX'
fi

file=$1

# Remove \r
tr -d '\r' < $file > $file.new
rm $file
mv $file.new $file

# replace \t with space
tr '\t' '    ' < $file > $file.new
rm $file
mv $file.new $file

if [[ $platform == 'MAC' ]]; then
  # On Mac, add trailing \n
  sed -i '' -e '$a\' $file
else
  sed -i -e '$a\' file
fi

Wednesday, November 26, 2014

Cygwin and Batch files

The two hardly mix, but sometimes they must. To invoke a batch file from cygwin run:

cmd /c mybatchfile.bat

And it's probably worth mentioning to invoke a batch file from a batch file use:

call mybatchfile.bat

Monday, September 29, 2014

jargrep Bash Script

When I work with Java it can be handy to grep for a string in a JAR, so I have created an executable script called "jargrep".

#!/bin/bash
# Greps a jar file for a string.


if [ $1 ] ; then
  find . -name "*.jar" -a -exec bash -c "jar tvf {} | grep $1" \; -print
else
  echo "Usage: jargrep [string]"
  echo "  Example: jargrep \"string to grep for\""
fi

Create a text file called jargrep, copy the contents above, then run "chmod +x jargrep" to make it executable.