Monthly Archives: December 2012

Useful VI commands

Show line numbers
:set number

Replacing a text within a file in place

Recently I had to replace some text within a file, I also wanted this to occur in place. I ended up with two solutions. Mainly because of the Solaris version. But also because I wanted to play with Groovy again.

1. Perl on the commandline

perl -pi -e 's/1.2-SNAPSHOT/1.2/g' pom.xml

2. Using Groovy
Refer to Groovy: Writing to a File

Unix find and grep

Periodically I need to find some files within a unix server that contain some random text. I also get caught out between say Redhat or Solaris or OSX.

Recently it was searching for some text within a heap of maven pom files.

find . -name pom.xml | xargs grep '1.2-SNAPSHOT'

Jenkins REST API

Jenkins has a useful REST api that can be used to create jobs, build and disable them.

Quick reference:

GET request to build

http://[userid]:[apitoken]@[jenkinsURL]/job/[Jenkins Job Name]/build

POST Request to Disable

http://[userid]:[apitoken]@[jenkinsURL]/job/[Jenkins Job Name]/disable

Groovy: Writing to a File

Recently had a requirement at work to update multiple maven pom files with the correct <version>. After performing a grep for the files that I needed to change I wrote this simple grroovy script to open each file replace the text and then save the file.

def fileName = "pom_location.txt" // Locations of the pom files
def sourceText = "1.2-SNAPSHOT"
def replaceText = "1.2"

new File(fileName).eachLine { line ->

def pomFile = line
pomFile = pomFile.replaceAll("\\s","") + "/pom.xml"
def file = new File(pomFile)
println "Editing ... $pomFile"
def fileText = file.text
fileText = fileText.replaceAll(sourceText, replaceText)

Thanks to David Heffelfinger

CURL Tutorial

I am always having to google a decent lookup on CURL.

Quick usage:

curl <url>