Thursday, December 29, 2011

Removing mutlitple spaces in a file with single space in Linux

Folks, it is time for some Linux commands.

Ok. Here is the one that I have used, to remove lot of spaces between words with a single space in a file.

cat MoreSpaces.txt | tr -s ' ' ' ' > OneSpace.txt

For example the "MoreSpaces.txt file is having the following text.

"Hello          This is      having      more spaces        between words..."

So, once you execute the above command the output file "OneSpace.txt" will be created and it's contents will be like as given below.

"Hello This is having more spaces between words..."

Sweet isn't it ???





Saturday, December 24, 2011

Passing more than 10 arugumets to Windows Batch script

Last week came across an interesting scenario while working a batch script with my friend.
That is, we need to pass more than 10 arguments to a batch script. There will be no problem to pass 9 arguments. I will just try to explain the normal way we use to get up to 9 arguments and way to get more than 10 arguments.

Up to 9 arguments

Consider a script test.bat being called with arguments, a b c  as given below.

c:\> test.bat a b c
Inside test.bat we will getting the arguments as given below.
@echo off
           set arg_1=%1
set arg_2=%2
set arg_3=%3
For more than 10 arguments

We can not use %10 to get the 10th argument. Well, here is the solution we have found.

---------------------------------------------------------------------------------------
@echo ON

setlocal EnableDelayedExpansion

set arg_1=
set arg_2=
set arg_3=
set arg_4=
set arg_5=
set arg_6=
set arg_7=
set arg_8=
set arg_9=
set arg_10=
 //Tricky part
set /a m=0
FOR  %%i IN (%*) DO ( 
   set /a m+=1 
set arg_!m!=%%i
)

---------------------------------------------------------------------------------------

-Krishna








Friday, December 23, 2011

Apple wax peeler

I love eating apples, but I am very much worried about the wax on the apple skin. Even if we try to wash it, the wax will not go off very easily and the apple will be looking as shiny as ever after that.

I normally use knife to take it off, but I lose considerable amount of apple as well with the skin. At the end I will be eating only 70% apple alone. Considering the cost of the apple(Rs 25 /apple) this loss bothered me as well.

Accidentally seen a wax peeler tool when I was in a shop near my house. The cost is just Rs 25. 
Interestingly I found it very useful as it is taking skin of the apple alone and leaving much for me to eat. Hmm Yummy !!!!! ..

So if you go to shop next time, get one and enjoy. Will post some images if possible.

-Krishna.





Wednesday, December 7, 2011

show create table tablename - useful SQL query

Hello,

This is a very useful query in I used in MySQL to find out the "create statement" that was used to create a table.

It helped me to copy and paste the table.

Syntax and sample output,

mysql> show create table User\G;

*************************** 1. row ***************************
       Table: User
Create Table: CREATE TABLE User (
  ID INT(11) default NULL auto_increment,
  NAME char(60) default NOT NULL,
  EMAIL char(100) default NULL,
  PRIMARY KEY (ID)
) ENGINE=MyISAM


Unlike the query "desc tablename", which will give you the table definition,

we can get the table definition and the create statement from the

"show create table  tablename" query.


Guys, post your cases where you find this query useful.


-Krishna.