I was really amazed to find out that C++ string class has no split method. There are plenty of accasions I needed to split a string by a given delimiter. Lets see how to split a string and put substrings in a to vector.
If you think incrementing varable start is ugly, you can use function find_first_not_of for itFriday, July 23, 2021
Monday, July 5, 2021
Wednesday, June 30, 2021
modify files using sed in linux
sed is a powerfull tool and you can find all details from here
delete a line containing a substring ("pattern")
sed -i '/pattern/d' filereplace a stringsed -e s/pattern//g -i fileusing i.backup will keep a backup file of original and it is safer.
sed -e "s/pattern//g" -i.backup file
Get parent directory name of shell script
pwd | rev | cut -d/ -f1 | rev
lets see arguments for cut used with above command
spliting delimeter is given to -d, in this case \
required argument is given to -f, in this case 1
rev is used to reverse the string because parent directory name is the first argumet from reverse of pwd.
Get substring of a string in shell
I wanted to get a only file names from the output of command find . -maxdepth 1 -type d
This returns a strings contains "./" at the begining.
./1-case
./2-case
./3-case
./4-case
./5-case
./6-case
to remove first two characters i used command cut. as follows
echo "./1-case" | cut -c3-
-c means return characters
3- means from 3rd character to end
other examples
-c3 will return only 3rd character
-c3-4 will return characters form 3rd to 4th
Friday, June 25, 2021
Get the all folder names in current directory
I wanted to copy a file from one location to multiple folders. We'll have to copy multiple times if we do it manally. Lets see how to use shell script to copy to every folder in one go using shell scripting.
main parts we need to do.
1. get all directories.
find . -maxdepth 1 -type d
2. loop through every directory name, returned from 1, and copy file to each folder.
using foreach loop in s shell (.csh)
> foreach dir ( ` find . -maxdepth 1 -type d` )
> foreach? cp ~/Scripts/convert.py $dir
> foreach? end
using for loop in bash shell (.sh)
Other commands to get all directory names
- ls -d */
- ls -l | grep `^d'
Monday, June 21, 2021
Overview of TDD and BDD
Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)
Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...
-
PPA stands for Personal Package Archive and it is a special software repository for uploading source packages to be built and published as a...
-
When we write a C++ program we use g++ -Wall -o sample sample.cpp command (file name is sample.cpp) to compile the program then we run the ...
-
In Java socket programming(Network programming) we basically study about how to implement programs that communicate over a network. When we ...