Showing posts with label Sed. Show all posts
Showing posts with label Sed. Show all posts

Friday 13 May 2016

Extract pom.xml version number using Shell script - Scenario 1

Guys, in todays post we'll look into an example where we want to extract version number from pom.xml of a Maven based project. In other words, all we have to do is extract text between 2 strings from a file. Quite obviously we would use sed for that. But let's look into a scenario where you have to pipe sed to other unix commands.

FYI this will work universally on Linux/Unix/Mac OS X flavors.

Let's say I have a pom.xml file of a Maven based project and I want to display only the version number from it. As you may be aware, we define the version number within the <version> </version> tags.

For e.g. my pom.xml looks something like this :


Here we want to extract only the version number from the file. i.e in this case I only want to display 1.0-SNAPSHOT.

Did you notice that in our file we have two lines of the <version></version> tags. One for our main project version and the other is for the dependency version. Now if we use sed to extract text between the "version" words, it will give output like this below.

sed -n '/version/,/version/p' pom.xml
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>

      <version>3.8.1</version>

So let's display only the topmost line with head -1. That should help narrow down our output.

sed -n '/version/,/version/p' pom.xml | head -1
  <version>1.0-SNAPSHOT</version>

Now all we need is to extract the version number. Let's cut the output characters as follows :

sed -n '/version/,/version/p' pom.xml | head -1 | cut -c 12-23
1.0-SNAPSHOT

Cheers guys! That's what we wanted right? Now we could perhaps develop a script around this. Maybe have a variable which stores this version number. Perhaps we could use this variable for some conditional checks. Something like this :

#!/bin/bash
# Program to extract version number from the pom.xml

i=`sed -n '/version/,/version/p' /Users/ironcladzone/workspace/MavenTest/testapp/pom.xml | head -1 | cut -c 12-23`

echo $i

if [ $i = "1.0-SNAPSHOT" ]; then

echo "Version 1.0 Development copy. Team A is working on it."
else

echo "That's not a Version 1.0 Dev copy"

fi

Output :

./ExtractVersionFromPom.sh
1.0-SNAPSHOT

Version 1.0 Development copy. Team A is working on it.

The possibilities are limitless! Until next time, ciao!

Tuesday 29 March 2016

Modify a part of file path using Sed in Mac OS X - Tricky Scenario

Lets look into a fairly simple task with a slight twist today - update/modify a part of the file paths mentioned within a file using Sed from command line in Unix/Mac OS X. Mark this as a tricky interview question.

Let's consider a simple example. For instance we have a text file named Filepaths.txt which has the following file path details :

/Users/ironcladzone/Downloads/file1.txt
/Users/ironcladzone/Downloads/image.jpg
/Users/ironcladzone/Downloads/presentation.ppt
/Users/ironcladzone/Downloads/Spreadsheet.xls
/Users/ironcladzone/Downloads/Audio.mp3
/Users/ironcladzone/Downloads/Movie.avi
/Users/Shared/Downloads/Test1
/Users/Shared/Downloads/ABC
/Users/ironcladzone/Downloads/MovieClip.mov

Here we want to replace only a part of the file path i.e replace all instances of the word"Downloads" with "Documents". Sure you can instantly do it in some text editor using "Replace All". But the catch is I want to replace the word Downloads with Documents if and only if it exists in the /Users/ironcladzone/Downloads/ path. I want to skip all other file paths. In this case I want the path /Users/Shared/Downloads/Test1 & /Users/Shared/Downloads/ABC to remain intact and unchanged.

So let's see how to do it from command line using a Sed regex.

Open up terminal and type this :

sed 's/\/Users\/ironcladzone\/Downloads/\/Users\/ironcladzone\/Documents/g' FilePaths.txt

So here's the output as expected. Cheers!

/Users/ironcladzone/Documents/file1.txt
/Users/ironcladzone/Documents/image.jpg
/Users/ironcladzone/Documents/presentation.ppt
/Users/ironcladzone/Documents/Spreadsheet.xls
/Users/ironcladzone/Documents/Audio.mp3
/Users/ironcladzone/Documents/Movie.avi
/Users/Shared/Downloads/Test1
/Users/Shared/Downloads/ABC
/Users/ironcladzone/Documents/MovieClip.mov
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker