Friday, June 22, 2007
Double brackets for arithmetic operation
Suppose we have to increment the value of a variable
Normal way:
$ a=10; a=`expr $a + 2` ; echo $a
12
Can also be done this way:
$ a=10; ((a=a+2)); echo $a
12
Sunday, June 17, 2007
Split PATH: AWK digest
This is my PATH env variable set to
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/home/jsaikia/impscripts/
Now to SPLIT the PATH:
$ echo $PATH | awk -F : '{print "PATH is set to"} {for (i=1;i<=NF;i++) {print "["i"]",$i}}'
PATH is set to
[1] /usr/local/bin
[2] /usr/bin
[3] /bin
[4] /usr/bin/X11
[5] /usr/games
[6] /home/jsaikia/impscripts/
Another simple way of doing it:
$ echo $PATH | tr ':' '\n' | awk '{print "["NR"]"$0}'
[1]/usr/local/bin
[2]/usr/bin
[3]/bin
[4]/usr/bin/X11
[5]/usr/games
[6]/home/jsaikia/impscripts/
Make this a function in .bash_profile or .profile.
mypath () {
echo $PATH | tr ':' '\n' | awk '{print "["NR"]"$0}'
}
which will split out the elements of a PATH, one per line.
$ mypath
[1]/usr/local/bin
[2]/usr/bin
[3]/bin
[4]/usr/bin/X11
[5]/usr/games
[6]/home/jsaikia/impscripts/
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/home/jsaikia/impscripts/
Now to SPLIT the PATH:
$ echo $PATH | awk -F : '{print "PATH is set to"} {for (i=1;i<=NF;i++) {print "["i"]",$i}}'
PATH is set to
[1] /usr/local/bin
[2] /usr/bin
[3] /bin
[4] /usr/bin/X11
[5] /usr/games
[6] /home/jsaikia/impscripts/
Another simple way of doing it:
$ echo $PATH | tr ':' '\n' | awk '{print "["NR"]"$0}'
[1]/usr/local/bin
[2]/usr/bin
[3]/bin
[4]/usr/bin/X11
[5]/usr/games
[6]/home/jsaikia/impscripts/
Make this a function in .bash_profile or .profile.
mypath () {
echo $PATH | tr ':' '\n' | awk '{print "["NR"]"$0}'
}
which will split out the elements of a PATH, one per line.
$ mypath
[1]/usr/local/bin
[2]/usr/bin
[3]/bin
[4]/usr/bin/X11
[5]/usr/games
[6]/home/jsaikia/impscripts/
Tuesday, June 12, 2007
Renaming files
Suppose you have to rename all your .cpp files to .cc in a directory and corresponding sub-dirs, the following one liners will help you doing that.
$ find . -name "*.cpp" -type f -print
./as.cpp
./dire/cd.cpp
./bs.cpp
$ find . -name "*.cpp" -type f -print -exec rename 's/\.cpp$/\.cc/' {} \;
$ find . -name "*.cc" -type f -print
./as.cc
./bs.cc
./dire/cd.cc
WAHOO Its done!
The above example is a recursive one , if you just want to rename files in a directory and not in its sub-dirs, then
$ ls *.cpp | while read file
> do
> mv $file `basename $file .cpp`.cc
> done
$ find . -name "*.cpp" -type f -print
./as.cpp
./dire/cd.cpp
./bs.cpp
$ find . -name "*.cpp" -type f -print -exec rename 's/\.cpp$/\.cc/' {} \;
$ find . -name "*.cc" -type f -print
./as.cc
./bs.cc
./dire/cd.cc
WAHOO Its done!
The above example is a recursive one , if you just want to rename files in a directory and not in its sub-dirs, then
$ ls *.cpp | while read file
> do
> mv $file `basename $file .cpp`.cc
> done
Add/Change/Insert lines to a file using sed
Using sed we can Add/Change/Insert lines in a file, I found it very useful, hope you too !
$ cat namedb.txt
Nina:London
Apen:India
Lokesh:India
#Add a line
a)
$ sed '
/Apen/ a\
Add this line after Apen
' namedb.txt
Output
Nina:London
Apen:India
Add this line after Apen
Lokesh:India
b)
$ sed '
2 a\
Add this line after 2nd line
' namedb.txt
Output
Nina:London
Apen:India
Add this line after 2nd line
Lokesh:India
#Insert a new line before
c)
$ sed '
/Apen/ i\
Insert this line after Apen
' namedb.txt
Output
Nina:London
Insert this line after Apen
Apen:India
Lokesh:India
Similarly one can mention the line number (case b above)
#Change a line
d)
$ sed '
/Apen/ c\
Change the line with Apen to this line
' namedb.txt
output
Nina:London
Change the line with Apen to this line
Lokesh:India
Similarly one can mention the line number to change (case b above)
$ cat namedb.txt
Nina:London
Apen:India
Lokesh:India
#Add a line
a)
$ sed '
/Apen/ a\
Add this line after Apen
' namedb.txt
Output
Nina:London
Apen:India
Add this line after Apen
Lokesh:India
b)
$ sed '
2 a\
Add this line after 2nd line
' namedb.txt
Output
Nina:London
Apen:India
Add this line after 2nd line
Lokesh:India
#Insert a new line before
c)
$ sed '
/Apen/ i\
Insert this line after Apen
' namedb.txt
Output
Nina:London
Insert this line after Apen
Apen:India
Lokesh:India
Similarly one can mention the line number (case b above)
#Change a line
d)
$ sed '
/Apen/ c\
Change the line with Apen to this line
' namedb.txt
output
Nina:London
Change the line with Apen to this line
Lokesh:India
Similarly one can mention the line number to change (case b above)
Subscribe to:
Posts (Atom)
© Jadu Saikia http://unstableme.blogspot.com
