Thursday, June 7, 2018

Getting every nth line from file

I had a problem recently where I wanted to extract every 5th line from file and I knew that every 10 lines data points to same 10 keys


awk 'NR%5 == 0' file.txt

gave me that




In general if we want to extract every nth line from  file with repeated section of length L

we can just do




awk 'NR%L == n' file.txt 



Assume that we have lines with every section of 5 lines 
awk 'NR%5 == 1' values | awk '{n += $1}; END{print n}'   # sum of every 1st line
awk 'NR%5 == 2' values | awk '{n += $1}; END{print n}'   # sum of every 2nd line
awk 'NR%5 == 3' values | awk '{n += $1}; END{print n}'   # sum of every 3rd line
awk 'NR%5 == 4' values | awk '{n += $1}; END{print n}'   # sum of every 4th line
awk 'NR%5 == 0' values | awk '{n += $1}; END{print n}'   # sum of every 5th line


Friday, June 1, 2018

How to prepend text to all files in a directory

find  ./ -type f | xargs sed -i '1i wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh \
chmod +x Anaconda3-5.1.0-Linux-x86_64.sh \
./Anaconda3-5.1.0-Linux-x86_64.sh -b  \
source ~/.bashrc' $1



find ./ -type f   #Give all the files
xargs  # run thefollowing command
$1 #will be the file path

Sunday, October 29, 2017

Deep Learning personal notes

1. For Gradient descent the initialization of weight could be anything including zero, since function is convex it will always converge to <global or local?> minima

2. How to check if the pytorch has mkl or not ?
>>> torch.has_mkl
True

Wednesday, December 9, 2015

Using pickle to se/de-rialize in python.

 # Save a dictionary into a pickle file.
 import pickle
 favorite_color = { "lion": "yellow", "kitty": "red" }
 pickle.dump( favorite_color, open( "save.p", "wb" ) )

 # Load the dictionary back from the pickle file.
 import pickle
 
 favorite_color = pickle.load( open( "save.p", "rb" ) )
 # favorite_color is now { "lion": "yellow", "kitty": "red" }