Replace a string with an environment variable.

30.4.13
Many times its required inside a shell script that you need to replace a string in a file with a value which is present in a environment variable.Most of the time people will face this in their daily work life if they work on unix exclusively.
Below is the way to do it.
echo $LINE | sed -e "s/12345678/${replace}/g"
Small test below:
export replace=987654321
echo X123456789X | sed "s/123456789/${replace}/"
X987654321X
Read more ...

Hiding a standard library function in c++

25.4.13
I have a function abs in one of the libraries and I wanted to use that function by linking to that library.But I am facing this error below:
function abs used is #define abs(x) (x > 0) ? x : -(x).
This is obviously from the standard includes that I used. So I found a some very nice ways to overcome this problem :

  • Wrapping the function name abs in parenthesis has resolved the problem right away.
    (abs)(arguments)

  • Using the preprocessor directives Before you call the function abc in the library.
    #undef abs


Read more ...

Print Lines having unique fields

23.4.13
I have a table in which most of the values in a given row are the same. What I want to pull out are any rows where at least one of the values is different.
For example I have a file:
one one one one two
two two two two two
three four four five
I found a nice trick for doing this:
awk '{f=$0;gsub($1,"")}NF{print f}' file
one one one one two
three four four five
First we store the line in original state f=$0 then we do a global substitution on everything matching the first field, if all fields are the same then nothing will be left therefor NF will be 0 and nothing will be printed else we print the original line.
Read more ...

Sorting a file with related lines

17.4.13
I have a file in which first row contains the number and second row contains a statement associated with it and so on like the below example:
12
stat1 
18
stat2
15
stat3
I need to print the output like sorting in descending order of numbers along with the statement related to it. So that the output should look like:
Time = 18
Stat = stat2
Time = 15
Stat = stat3
Time = 12
Stat = stat1
Below is the perl command that I have written which does the needed thing.
perl -lne 'if(/^\d+/){$k=$_}
           else{$x{$k}=$_}
           END{ 
               for(sort {$b<=>$a} keys %x){ 
               print "time=$_\nStat=$x{$_}";}
           }' file
time=18
Stat=stat2
time=15
Stat=stat3
time=12
Stat=stat1
Note:Precondition is time will come first and then the statement associated with it. 
Below is the explanation.

1. So store in a variable $k if the line start with a decimal point number.
2. Else case it is nothing but the statement. So store this line as a value to the previous stored $k.
   After parsing complete file.sort the keys of the hash in descending order by
3. sort  $b<=>$a and then print each and ever key and value in needed format.
Also please note:
1. sort $a<=>$b will sort the numbers in ascending order.
2. sort $b<=>$a will sort the numbers in descending order
Read more ...