Tuesday, May 19, 2020

Linux Shell - What is IFS?

 IFS stands for internal field separator. This is the delimiter used when words are split.

The man page of bash tells :

IFS    The Internal Field Separator that is used for word splitting after expansion and to split lines into
              words with the read builtin command.  The default value is ``<space><tab><newline>''.

The default value of IFS is a space, a tab followed by a newline.
guru@unixschool:~$ echo "$IFS"
  

guru@unixschool:~$ echo "$IFS" | cat -tve
 ^I$
$
guru@unixschool:~$ 
  When we echoed IFS for the first time, we could not see anything becuase they are special characters. On using the tve options of cat, we can see a space, followed by a ^I which is a tab character and then followed by a newline.

Thursday, May 14, 2020

Python - How does an iterator work

 In this article, we will discuss how does an iterator work.  Python has 2 function related to iterator: iter and next. iter creates an iteration object for the requested input, and the next function returns the next element present in the iterator. next keeps on returning till the last element is reached.

   Let us create an iterator for a list and see how the next and iter function works:
>>> l1 = [2,25,33,12]
>>> l1
[2, 25, 33, 12]
>>> it1 = iter(l1)
>>> it1
<list_iterator object at 0x7fc24f93aa58>
>>> next(it1)
2
>>> next(it1)
25
>>> next(it1)
33
>>> next(it1)
12
>>> next(it1)
Traceback (most recent call last):
  File "", line 1, in 
StopIteration
>>> 
When we printed it1, it shows it as an list_iterator object. Everytime next is hit, it gave the next element and finally when there are no more elements, it gives StopIteration.

Tuesday, May 5, 2020

bash - 5 examples to do arithmetic operations

 While working in bash or while writing bash scripts, many times it so happens we rely on external commands to get our math done. Most of the arithmetic can be handled by the bash itself.   In this article, we will see how to do arithmetic operations in bash.

1. Add / Subtract numbers:
     One common way to basic arithmetic using bash is by using the let command.
x=20
y=30
let z=x+y
echo "z is $z"
  This is purely a shell operation without any external command. This way one can do for subtract(/), multiply(*), divide(/) and remainder(%) operation.
x=20
y=30
((z=x+y))
echo "z is $z"
  The other way to do is using the ((expression)) where the expression is an arithmetic one. Using this double brace notation, all the bash arithmetic can be done.
   Bash also allows to use +=, -= operators like in many programming languages. So, if we have something like x=x+10, it can be written as
x=40
((x+=10))
echo "x is $x"

Thursday, April 30, 2020

Python - How to read a file?

   In this article, we will see how to read from a file. Let us have file with content as below:
$ cat file
Unix
Linux
AIX
1.  Reading line by line:
>>> fd = open('/home/guru/file', 'r')
>>> for line in fd:
...   print(line)
... 
Unix

Linux

AIX

>>> 
         open function opens a file and returns a file object.  Arguments are filename and the mode, 'r' for reading.  When the file object is traversed, every iteration reads a line and line gets printed. Notice the blank line after every print. This is because the variable line contains a newline character read from the file and print function adds another newline.

Tuesday, April 28, 2020

bash - 10 examples to find / replace in a string

Many times when we want to replace or extract something, we immediately end up with and awk or a sed oneliner. Keep in mind, the first option should always be a internal shell option, only in the absence of which we should resort to an external command. In this article, we will see 10 different examples where instead of an awk/sed, using bash specific internal will be very beneficial:

1. Capitalize a string:  Though there are many ways to capitalize a string,  one tends to use toupper function to get it done. Instead, bash can directly handle it.
$ x='hello'
$ echo ${x^^}
HELLO
$ echo "$x" | awk '{print toupper($0)}'
HELLO
   This feature of bash(^^) is only available for bash version 4. The two carrot pattern capitalizes the entire string.

Thursday, April 23, 2020

Python - How to get the NSE daily data feed?

These are days when trading decisions are taken more by a software, and people write algorithms to get their trades executed. For all this to happen, you need to have the stock data feed with you. Data is nothing but the daily open,close,high, low and volume for the stocks. Once you have the feed, the kind of things you can do with that data is endless.  In this article, we will see how to download the data feed through a Python program.

Pre-requisites:
1. Install pandas if not present already:
pip install pandas
2. Alpha Vantage has exposed free API's for downloading data feed. Install the alpha vantage package:
pip install alpha-vantage
3. You need to register yourself in Alpha vantage and get an API key.

Saturday, April 11, 2020

Python - Get nth prime number

 Let us try to solve the 7th problem in Euler's project which is to get the 10,001st prime number. We will start with writing a function to check if a number is prime or not.

from math import sqrt

def isPrime(x):
    if x < 2:
        return False
    if x == 2 or x == 3:
        return True
    for i in range(2, int(sqrt(x)) + 1):
        if x % i == 0:
            return False

    return True