Tuesday 16 February 2016

Add a character at a specific location within strings using Awk

Folks, in previous examples we saw how to add a prefix to strings in a file and how to add a suffix to strings in a file. In today's example, we'll see how to add a character somewhere in the middle of multiple strings in a file at the same time.

We'll use the same data as used in previous examples. Let's use a text file named Phones_unformatted.txt with the following phone numbers data :

2127841212
3458922345
7713398403
6461228847

Now we want to add a - sign after the first 3 digits to separate the area code. Notice that all phone numbers are of 10 digits. We want to add a - sign between the 3rd and 4th character.

For doing this lets use awk again :

awk '$0=substr($0,1,3) "-" substr($0,4,7)' Phones_unformatted.txt

Output :

212-7841212
345-8922345
771-3398403
646-1228847


Notice how we have used substr() function to split the entire 10 digit phone number into 2 parts. The first part is between 1-3rd character and the second part is from 4-10th character.

Similarly, lets say for e.g. we want to add the - sign twice within the string i.e once between 3rd and 4th character and the next - between 6th and 7th character. The awk code for doing this is as follows :

awk '$0=substr($0,1,3) "-" substr($0,4,3) "-" substr($0,7,4)' Phones_unformatted.txt

Output :

212-784-1212
345-892-2345
771-339-8403
646-122-8847

This way, you could append the string with the "-" character or anything like a dot "." or maybe just a whitespace " ".

awk '$0=substr($0,1,3) " " substr($0,4,3) " " substr($0,7,4)' Phones_unformatted.txt

Output :

212 784 1212
345 892 2345
771 339 8403
646 122 8847

List all baselines of a stream in IBM RTC

For listing out all baselines of a stream using the Rational Team Concert command line interface, type the following command.

Usage :

lscm list baselines -r repository-name -w stream-name

Eg :

lscm list baselines -r https://jtsrv.uk.hibm.company:9094/jazz -w htse.mydocs.inc.midata.release

Notice we haven't used any double quotes for any parameter in the syntax for this command.

Sunday 14 February 2016

Kala Ghoda Festival 2016, Mumbai : PhotoStream

Hello fellas! Presenting an artsy post today featuring the Kala Ghoda arts festival 2016, Mumbai. FYI, Kala Ghoda literally means Black Horse.

Check out the raw unedited PhotoStream here. (Pardon the photo quality since its taken with a cheap phone camera)

The festival which is held every here in the Kala Ghoda area of Fort neighborhood, Mumbai. It features a variety of entertainment programs like :
  • Street art fest featuring unique sculptures and installations.
  • Poetry and book reading sessions
  • Street plays and dramas
  • Music shows
  • Workshops
  • Theatre shows
  • Food and cuisine tasting sessions and much much more...
The official schedule can be found here. Also do check this map for a quick location guide. Make sure you visit it since 14th Feb is the last closing day, in case you missed it.

Print File size using Java

If you ever want to print the file size using Java, use the following piece of code below :

import java.io.*;

public class FileSize {

public static void main(String[] args) {
File f = new File("//Users//ironcladzone//Downloads//image1.jpg");
System.out.println("File size in bytes = " +f.length());
System.out.println("\nFile size in kb = " +f.length()/1024);

}


}

Output :

File size in bytes = 241833

File size in kb = 236

Note that when we use the length() function, it gives us the file size in bytes. For getting output in kilobytes, divide it by 1024. If you want size in megabytes, again divide it by another 1024. So on and so forth...

Add suffix to strings in a file using Awk

Guys, in my previous post yesterday, we discussed how to add a prefix to strings in a file using Awk. Today lets work on the same example to add a suffix to the strings i.e append the strings with characters in the end.

We'll use the same set of phone numbers for formatting :

2127841212
3458922345
7713398403
6461228847

Let's add some characters to the these numbers in the end. How about something like " Extn : 7142". Here's the Awk code to do it below :

awk '{print $0 " Extn : 7142"}' Phones_unformatted.txt

Output :

2127841212 Extn : 7142
3458922345 Extn : 7142
7713398403 Extn : 7142

6461228847 Extn : 7142

Saturday 13 February 2016

Add a prefix to strings in a file using Awk

Suppose you have a file in Unix / Mac OS X which has phone numbers data. I mean lets say I have a text file named Phones_unformatted.txt which has some phone numbers as follows :

2127841212
3458922345
7713398403
6461228847

Now I want to format the phone numbers to have a prefix maybe like a country code +44- . something like :

+44-2127841212
+44-3458922345
+44-7713398403
+44-6461228847

If you observe I need to only append the phone number with some characters in the beginning. We can achieve the same using awk.

A simple awk code will do the trick as follows :

awk '{print "+44-" $0}' Phones_unformatted.txt

Output :

+44-2127841212
+44-3458922345
+44-7713398403
+44-6461228847

Viola!

Create a new directory using Java

In today's example we'll create a new directory using Java. Check out the following code below :

import java.io.*;

public class Create_Directory {

public static void main(String[] args) {
File dir = new File("//Users//ironcladzone//Downloads//TestDir");
boolean DirectoryCreated = dir.mkdir();
if (DirectoryCreated)
System.out.println("Created a new directory");
else
System.out.println("Directory not created");
}


}

Note that we have used the function mkdir() as expected. It returns the value "true" if the directory is created or "false" if not created. We use the variable 'DirectoryCreated' to store this boolean value, based on which it prints a message.

Try executing this program twice. The first time when you execute the program, a directory gets created and you get the output like :

Created a new directory

The second time when you execute the code, a directory does not get created since it already exists. So you'll get the output like :

Directory not created
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker