PySpark, BigData, SQL, Hive, AWS, Python, Unix/Linux, Shortcuts, Examples, Scripts, Perl
Jan 2, 2013
Dict keys and values in Python
Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.
Keys are unique in Dict, Values are not necessarily unique in nature.
relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
for member in sorted(relatives.keys()):
#print "\n",member
print("\n",member)
for member in sorted(relatives.values()):
#print "\n",member
print("\n",member)
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Dict Example in Python
Today, we discuss about the Dict concept in Python.
Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.
Keys are unique in Dict, Values are not necessarily unique in nature.
Let's discuss with an example, let's dive & have fun :
relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
#Keys are unique
relatives['Marge'] = "mother";
relatives['marge'] = "mother1";
#'Marge' and 'marge' are different keys
for member in sorted(relatives.keys()):
print("\n",member, "=>", relatives[member])
#print "\n",member
for member in sorted(relatives.values()):
print("\n",member)
#print "\n",member
for key, value in relatives.items():
print("\n", key, "=>", value)
#print "\n", key, "=>", value
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Dict in Python consists of Key & Value pairs. This is similar to Hash concept in Perl language.
Keys are unique in Dict, Values are not necessarily unique in nature.
Let's discuss with an example, let's dive & have fun :
relatives = {"Lisa" : "daughter", "Bart" : "son", "Marge" : "mother", "Homer" : "father", "Santa" : "dog"}
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
#Keys are unique
relatives['Marge'] = "mother";
relatives['marge'] = "mother1";
#'Marge' and 'marge' are different keys
for member in sorted(relatives.keys()):
print("\n",member, "=>", relatives[member])
#print "\n",member
for member in sorted(relatives.values()):
print("\n",member)
#print "\n",member
for key, value in relatives.items():
print("\n", key, "=>", value)
#print "\n", key, "=>", value
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Append to list in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
new_insp_ppl = "prabhath"
inspiring_ppl.append(new_insp_ppl)
o/p:
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "prabhath"]
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Delete the last name from the list in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Abdul Kalam"]
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
del inspiring_ppl[-1] #removes Abdul Kalam
print ("The following inspiring_ppl are :", inspiring_ppl)
#print "The following inspiring_ppl are :", inspiring_ppl
#o/p:
#inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Remove an element from List in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
new_inspiring_ppl = inspiring_ppl[:]
#Note:
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
for person in new_inspiring_ppl:
print("Do you want to keep person", person, "?")
#print "Do you want to keep person", person, "?"
answer = input("yes/no ")
#answer = raw_input ("yes/no ")
if answer != "yes":
inspiring_ppl.remove(person)
print(inspiring_ppl)
#print inspiring_ppl
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Python difference between list copy and reference
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
#The following Shares same memory location
inspiring_ppl_1 = inspiring_ppl
inspiring_ppl.append("Abdul Kalam")
inspiring_ppl_1.append("Abraham Lincoln")
#Therefore inspiring_ppl, inspiring_ppl_1 contains the same contents including "Abdul Kalam" and "Abraham Lincoln"
#Python does not have variables like C
#In Python variables are just tags attached to objects
#The following shares different memory location
inspiring_ppl_2 = inspiring_ppl[:]
inspiring_ppl.append("Swami Vivekananda")
print(inspiring_ppl)
o/p: ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Abdul Kalam", "Abraham Lincoln"]
print(inspiring_ppl_1)
o/p: ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Abdul Kalam", "Abraham Lincoln"]
print(inspiring_ppl_2)
o/p: ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs", "Swami Vivekananda"]
Check an element exists in an array/list in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
#raw_input(), print for Python 2.X Version
#input(), print() for Python 3.X Version
another_person = input("Enter person Name : ")
if another_person in inspiring_ppl:
inspiring_ppl.remove(another_person)
else:
inspiring_ppl.append(another_person)
print(inspiring_ppl)
How to copy a list into another
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
more_inspiring_ppl = inspiring_ppl[:]
more_inspiring_ppl.reverse()
print inspiring_ppl #2.X Version
#print (inspiring_ppl) #3.X Version
print more_inspiring_ppl #2.X Version
#print (more_inspiring_ppl) #3.X Version
For Loop in Python
inspiring_ppl = ["Mahatma Gandhi", "Mother Teresa", "Paul Coelho", "Bill Gates", "Steve Jobs"]
for person in inspiring_ppl:
print "Hello", person + ", how are you?" #3.X Version
#print("Hello", person + ", how are you?") #2.X Version
Reading a File with line numbers
file = open("read.txt","r")
text = file.readlines()
file.close()
counter = 1
for line in text:
print counter, line,
counter = counter +1
File Operations in Python
file = open("read.txt","r")
text = file.readlines() #Read lines from read.txt
file.close()
file2 = open ("write.txt", "w")
#Write all lines from read.txt into write.txt
#It overrides if any content already present in write.txt
file2.writelines(text)
file2.close
file2 = open ("append.txt", "a")
#It appends content from read.txt into append.txt
file2.writelines(text)
file2.close
Print File Content in Python
file = open("read.txt","r")
text = file.readlines() #Reads the file content
file.close()
for line in text:
print line #Python 2.X Versions
#print(line) #Python 3.X Versions
Print File in Reverse Order in Python
file = open("read.txt","r")
text = file.readlines() #Reads the file content
file.close()
text.reverse() #It reverses the content
for line in text:
print line #Python 2.X Versions
#print(line) #Python 3.X Versions
Labels:
python,
python_basics,
python_file,
python_scripts
Dec 30, 2012
Unix Shortcuts 1
a) !! #to run the previous command
e.g.,
>>pwd
>>!! #executes "pwd" command
b) Control+P #move up through history list, one command at a time. (Up arrow key)
c) Control+N #move down (or Down arrow key).
d) !n #run nth command in history list. where n is a number from your history list.
e.g.,
>>history
1 ls
2 pwd
3 vim test.txt
4 vim ~/aliases
5 vim ~/.vimrc
6 history
>>!3 #runs 3rd command in the history i.e., vim test.txt
>>!-2 #runs 2nd command from the end of history i.e., vim ~/.vimrc
e) !n:p #only preview command number n (will not execute the command).
f) !string #Run most recent command starting with characters in string.
e.g.,
>>vim text.txt
>>pwd
>>ls -lrt
>>mkdir test
>>!vim press<Enter> #Runs most recent command starting with "vim"
>>!ls press<Enter> #Runs most recent command starting with "ls"
g) Move the cursor to beginning of line in Unix Terminal
Beginning of line Ctrl+a
h) Move the cursor to end of line in Unix Terminal
End of line Ctrl+e
Find Files Using a Wildcard in UNIX
- Find command checks recursively in all the folders present with in that path
find files ending with "py" in current directory recursively
find . -iname \*.py
find files starting with prel_log in "/" directory recursively
find / -name perl_log\* -print
- The backslash \ character is important
- It tells the shell not to treat the wildcard character as a wildcard.
Execte the Perl script within the edited perl file
Suppose you edited a perl file in Vim editor
You can execute the perl script there itself without going back to command prompt, which saves you time
e.g.,
vim test.txt
press <Escape>
! perl %
- It executes the script which already edited
- if you want to execute a different script, mention the file name
press <Escape>
! perl test2.txt
- press Enter again to return to the edited file
Dec 17, 2012
vim edit multiple files
How to open multiple files in different tabs in Vim
]# vim -p file1.txt file2.txt file3.txt
How to access multiple files from the same window
]# vim test_1.txt #Which opens test_1.txt
After editing this file, without closing or coming out of this file, do you know how to reopen a file.
:e test_2.txt #Which opens test_2.txt
Now you want to reopen the previous file test_1.txt
:e# #Now you have already opened test_2.txt, but this reopens test_1.txt
after this, if you try to give the command again, it reopens previous file test_2.txt
:e# #Now you have already opened test_1.txt, but this reopens test_2.txt
Suppose if you want to open some other file, just give the file name and access
:e test_3.txt
This trick helps you a lot when you have to work switching back to back two files and this way you can easily reopen the previous file without much typing effort
Want to learn more about Vim/Vi Commands, please refer to the following:
http://perlexamples.blogspot.mx/2012/11/vim-commands.html
Dec 5, 2012
Unix How to Find a Particular Directory
We know how to find a file in Unix directories using
This will search sample.txt in current directory
#]find . -iname sampe.txt
But if we want to find out a particular directory in the unix, type -d denotes search for directory
#] find / -name mysql -type d
/var/log/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/etc/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/share/mysql
In order to narrow down the search, you could mention exact path (/etc) where you want to search
#] find /etc -name mysql -type d
/etc/mysql
Unix For Loop
#]mkdir for_test_dir1
#]mkdir for_test_dir2
#]mkdir for_test_dir3
#]cat for_test_dir1/for_test.txt
content 1
content 1
content 1
#]cat for_test_dir2/for_test.txt
content 2
content 2
content 2
#]cat for_test_dir3/for_test.txt
content 3
content 3
content 3
...
...
#] ls
for_test_dir1 for_test_dir2 for_test_dir3 for_test_dir4 for_test_dir5
......
......
#]foreach var (`ls`)
foreach? ls -ltr $var/for_test*
foreach? end
O/P:
It will list all the for_test files present in for_test_dir's
Dec 1, 2012
pushd popd example
When you have to work with navigate different directories quickly, its difficult to remember the whole path or type the whole thing again and again.
There is concept in Linux/Unix to manipulate directory stack, you can use directory stack to push directories into it and later pop directory from the stack.
1) dirs : Display the current directory stack without pushing or popping a new directory
2) pushd : Push directory into the Stack
3) popd : Pop directory from the Stack
Using pushd and popd effectively can help you get around your command-line environment quickly
E.g.,
]# mkdir /usr/local/dir1
]# mkdir /usr/local/dir2
]# mkdir /usr/local/dir3
]# dir
~
]# pushd /usr/local/dir1
]# dir
/usr/local/dir1 ~
]# pushd /usr/local/dir2
]# dir
/usr/local/dir2 /usr/local/dir1 ~
]# pushd /usr/local/dir3
]# dir
/usr/local/dir3 /usr/local/dir2 /usr/local/dir1 ~
]# popd
]# pwd
/usr/local/dir3
]# dir
/usr/local/dir2 /usr/local/dir1 ~
]# popd
]# pwd
/usr/local/dir2
]# dir
/usr/local/dir1 ~
]# popd
]# pwd
/usr/local/dir1
]# dir
~
There is concept in Linux/Unix to manipulate directory stack, you can use directory stack to push directories into it and later pop directory from the stack.
1) dirs : Display the current directory stack without pushing or popping a new directory
2) pushd : Push directory into the Stack
3) popd : Pop directory from the Stack
Using pushd and popd effectively can help you get around your command-line environment quickly
E.g.,
]# mkdir /usr/local/dir1
]# mkdir /usr/local/dir2
]# mkdir /usr/local/dir3
]# dir
~
]# pushd /usr/local/dir1
]# dir
/usr/local/dir1 ~
]# pushd /usr/local/dir2
]# dir
/usr/local/dir2 /usr/local/dir1 ~
]# pushd /usr/local/dir3
]# dir
/usr/local/dir3 /usr/local/dir2 /usr/local/dir1 ~
]# popd
]# pwd
/usr/local/dir3
]# dir
/usr/local/dir2 /usr/local/dir1 ~
]# popd
]# pwd
/usr/local/dir2
]# dir
/usr/local/dir1 ~
]# popd
]# pwd
/usr/local/dir1
]# dir
~
Nov 28, 2012
How to find the size of a directory in linux
We all know "du" command gives the size of the files, but it not convenient to read or understand the size as "du" command gives the size in KB's
In order to make is human readable format, please follow as mentioned below
du -lsh <directory_name>
du -lsh <file_name>
-h => display the size in human readable format
e.g.,
>> du -lsh prabhath_movies
5GB prabhath_movies
>> du -lsh hero.avi
1GB hero.avi
Thanks for going through this article, Have a good day :-)
In order to make is human readable format, please follow as mentioned below
du -lsh <directory_name>
du -lsh <file_name>
-h => display the size in human readable format
e.g.,
>> du -lsh prabhath_movies
5GB prabhath_movies
>> du -lsh hero.avi
1GB hero.avi
Thanks for going through this article, Have a good day :-)
Vim Commands
1) How to open file in VI Editor at a specific line
]# vi Sample.txt ++105
ie., From the linux terminal, it will open the Sample.txt at line 105
2) How can we search word that is under the cursor in VI/VIM
Put the cursor on the word
Press " * " for the next occurrence of the word
3) a) Replace a pattern with another pattern from line "m" to line "n"
>>cat test.txt
...
49 prabhath kota
50 lakshmi muvvala
51 vamsidar kota
52 prabhath kota
53 prabhath prabhath
54 perl awk
55 prabhath prabhath
56 kota prabhath
...
:50,55s/prabhath/darling/
#It replaces line 50 to line 55, only the first occurance in the line
o/p:
...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 darling kota
53 darling kota
54 perl awk
55 darling kota //replaced only first occurance
56 kota prabhath #56th line
...
>>cat test.txt
...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 prabhath kota
53 prabhath prabhath //replaced all
54 perl awk
55 prabhath prabhath //replaced all
56 kota prabhath #56th line
...
:50,55s/prabhath/darling/g
#It replaces line 50 to line 55, all the occurances in the line
o/p:
...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 darling kota
53 darling darling
54 perl awk
55 darling darling
56 kota prabhath #56th line
...
3 b) Replace a pattern with another pattern replacing through out the file
:%s/prabhath/prabhath12345/g
3 c) Replace a pattern with another pattern from the current line to next "n" lines
e.g.,
>> cat test.txt
test test test test
test test test test
#if the cursor is on the 2nd line ("." denotes 2nd line)
#if the cursor is on the 1st line ("." denotes 1st line)
:.,.+2s/test/testabc
o/p:
testabc test test test
testabc test test test
:.,.+2s/test/testabc/g
# "g" is for all the occurances in the current line
o/p:
testabc testabc testabc testabc
testabc testabc testabc testabc
4) To remove the last character from every line in the file.
Use it if every line in the file ends with ^M as the result of a file transfer.
Execute it when the cursor is on the first line of the file.
:%s/.$//
Note:
$ -> denotes end of the line
^ -> denotes start of the line
5) To remove the first character from every line in the file.
:%s/^.//
Note:
$ -> denotes end of the line
^ -> denotes start of the line
6) How to search in Vim
" Show all tabs:
/\t
" Show and Replace trailing(ending) whitespace:
:%s/\s\+$//gc
" Show spaces before a tab:
/\+\ze\t
7) Edit new file from the current viewing of the file...
Suppose you already opened test.txt, suppose you are trying to open new.txt now..
gvim test.txt
after that you want to close test.txt and open new.txt, then you can do like...
:e new.txt
8) How to access multiple files from the same window
]# vim test_1.txt #Which opens test_1.txt
After editing this file, without closing or coming out of this file, do you know how to edit another file from the same window
:e test_2.txt #Which opens test_2.txt
Now you want to go reopen the previous file test_1.txt
:e# #Now you have already opened test_2.txt, but this reopens test_1.txt
after this, if you try to give the command again, it reopens previous file test_2.txt
:e# ##Now you have already opened test_1.txt, but this reopens test_2.txt
This trick helps you a lot when you have to work switching back to back two files and this way you can easily reopen the previous file without much typing effort
9) How to Split screen
Horizontally:
vi test.txt
:split new.txt
Vertically:
vi test.txt
:vsplit new.txt
Note:
Cntrl + ww //To change the window from one to another
9) Move the cursor to beginning of line in Vim Editor
$ takes you to the end of the line
10) Move the cursor to beginning of line in Vim Editor
^ takes you to the first non-whitespace character in the line, and
0 (zero) takes you to the beginning of the line including whitespace
]# vi Sample.txt ++105
ie., From the linux terminal, it will open the Sample.txt at line 105
2) How can we search word that is under the cursor in VI/VIM
Put the cursor on the word
Press " * " for the next occurrence of the word
3) a) Replace a pattern with another pattern from line "m" to line "n"
>>cat test.txt
...
49 prabhath kota
50 lakshmi muvvala
51 vamsidar kota
52 prabhath kota
53 prabhath prabhath
54 perl awk
55 prabhath prabhath
56 kota prabhath
...
:50,55s/prabhath/darling/
#It replaces line 50 to line 55, only the first occurance in the line
o/p:
...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 darling kota
53 darling kota
54 perl awk
55 darling kota //replaced only first occurance
56 kota prabhath #56th line
...
>>cat test.txt
...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 prabhath kota
53 prabhath prabhath //replaced all
54 perl awk
55 prabhath prabhath //replaced all
56 kota prabhath #56th line
...
:50,55s/prabhath/darling/g
#It replaces line 50 to line 55, all the occurances in the line
o/p:
...
49 prabhath kota #49th line
50 lakshmi muvvala
51 vamsidar kota
52 darling kota
53 darling darling
54 perl awk
55 darling darling
56 kota prabhath #56th line
...
3 b) Replace a pattern with another pattern replacing through out the file
:%s/prabhath/prabhath12345/g
3 c) Replace a pattern with another pattern from the current line to next "n" lines
e.g.,
>> cat test.txt
test test test test
test test test test
#if the cursor is on the 2nd line ("." denotes 2nd line)
#if the cursor is on the 1st line ("." denotes 1st line)
:.,.+2s/test/testabc
o/p:
testabc test test test
testabc test test test
:.,.+2s/test/testabc/g
# "g" is for all the occurances in the current line
o/p:
testabc testabc testabc testabc
testabc testabc testabc testabc
4) To remove the last character from every line in the file.
Use it if every line in the file ends with ^M as the result of a file transfer.
Execute it when the cursor is on the first line of the file.
:%s/.$//
Note:
$ -> denotes end of the line
^ -> denotes start of the line
5) To remove the first character from every line in the file.
:%s/^.//
Note:
$ -> denotes end of the line
^ -> denotes start of the line
6) How to search in Vim
" Show all tabs:
/\t
" Show and Replace trailing(ending) whitespace:
:%s/\s\+$//gc
" Show spaces before a tab:
/\+\ze\t
7) Edit new file from the current viewing of the file...
Suppose you already opened test.txt, suppose you are trying to open new.txt now..
gvim test.txt
after that you want to close test.txt and open new.txt, then you can do like...
:e new.txt
8) How to access multiple files from the same window
]# vim test_1.txt #Which opens test_1.txt
After editing this file, without closing or coming out of this file, do you know how to edit another file from the same window
:e test_2.txt #Which opens test_2.txt
Now you want to go reopen the previous file test_1.txt
:e# #Now you have already opened test_2.txt, but this reopens test_1.txt
after this, if you try to give the command again, it reopens previous file test_2.txt
:e# ##Now you have already opened test_1.txt, but this reopens test_2.txt
This trick helps you a lot when you have to work switching back to back two files and this way you can easily reopen the previous file without much typing effort
9) How to Split screen
Horizontally:
vi test.txt
:split new.txt
Vertically:
vi test.txt
:vsplit new.txt
Note:
Cntrl + ww //To change the window from one to another
9) Move the cursor to beginning of line in Vim Editor
$ takes you to the end of the line
10) Move the cursor to beginning of line in Vim Editor
^ takes you to the first non-whitespace character in the line, and
0 (zero) takes you to the beginning of the line including whitespace
Cron Job Basics - Crontab
crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
min 0-59
hour 0-23
day 1-31
mon 1-12
dow 0-6 (Sunday = 0, Saturday = 6) DOW => Day of Week
min hr day month Week
30 00 01 01,06,12 * –> 00:30 Hrs on 1st of Jan, June & Dec.
00 20 * 10 01-05 –> 8.00 PM every weekday (Mon-Fri) only in Oct
00 00 01,10,15 * * –> midnight on 1st ,10th & 15th of month
05,10 00 10 * 01 –> At 12.05,12.10 every Monday & on 10th of every month
05 00 01 01 * -> 00:05 Hrs on 1st of Januray of each year
The asterisk in the field indicates to cron that any day of the week is acceptable.
However, since an explicit month and day have been specified, the day of the week is effectively ignored.
How to create a cron job that will run on the last day of each month, considering the fact that a month could have 28, 29, 30 or 31 days ?
Ans:
Your script, however, can test for that and exit if it is not the last day.
So - you'd set it in cron to run on the 28, 29, 30 and 31. Your script decides if it really is the last day and exits if it is not.
Or you can put each month into crontab separately and just deal with February in your script.
min hr day month Week
30 20 28,29 02 * /usr/prabhath/run.pl -> 8.30 PM every 28th of Feb
30 20 30 04,06,09,11 * /usr/prabhath/run.pl -> 8.30 PM every 30th of Months having 30 days
30 20 31 01,03,05,07,08,10,12 * /usr/prabhath/run.pl -> 8.30 PM every 31th of Months having 31 days
More Examples:
################
min hr day month Week User Command
01 * * * * root echo "This command is run at one min past every hour"
17 08 * * * root echo "This command is run daily at 8:17 am"
17 20 * * * root echo "This command is run daily at 8:17 pm"
00 04 * * 0 root echo "This command is run at 4 am every Sunday"
42 04 01 * * root echo "This command is run 4:42 am every 1st of the month"
01 * 19 07 * root echo "This command is run hourly on the 19th of July"
Example:
############
min hr day month Week User Command
* 12 16 * Mon root cmd
Will run cmd at midday every Monday and every 16th, and will produce the
same result as both of these entries put together would:
* 12 16 * * root cmd
* 12 * * Mon root cmd
Example:
############
min hr day month Week User Command
59 11 * * 1,2,3,4,5 root backup.sh
equals to
59 11 * * 1-5 root backup.sh
Step Values:
#################
Cron also supports 'step' values.
*/2 in the dom field would mean the command runs every two days and likewise,
*/5 in the hours field would mean the command runs every 5 hours.
Example:
############
min hr day month Week User Command
* 12 10-16/2 * * root backup.sh
is the same as:
* 12 10,12,14,16 * * root backup.sh
E.g.,
min hr day month Week User Command
*/15 9-17 * * * root connection.test
Will run connection.test every 15 mins between the hours or 9am and 5pm
min hr day month Week User Command
* 12 1-15,17,20-25 * * root cmd
Will run cmd every midday between the 1st and the 15th as well as the 20th and 25th (inclusive) and also on the 17th of every month.
O/P from Cron:
###################
min hr day month Week User Command
*/15 9-17 * * * root connection.test | mail -s "Subject" prabhathkota@gmail.com
*/15 9-17 * * * root connection.test >/dev/null 2>&1 #It won't send mail to anyone
*/15 9-17 * * * root connection.test > log.file
*/15 9-17 * * * root connection.test > log.file 2>&1
*/15 9-17 * * * root connection.test > mail -s "logfile for cmd" <log.file
*/15 9-17 * * * root connection.test >
Disable Email in Cronjob
#########################
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
Word Count - Unix
>> wc test.txt
15 43 282 test.txt
ie.,
15 lines
43 words
282 chars
>>wc -l test.txt #Lines
15 test.txt
>> wc -c test.txt #Words
282 test.txt
>> wc -w test.txt #Characters
43 test.txt
Labels:
command,
command line,
count,
unix,
unix commands,
word count
Nov 25, 2012
How to print first two columns of a file - AWK
1) It will print first line alone
>>awk 'NR==1' test.txt #1st line from the top
>>awk 'NR==7' test.txt #7th line from the top
2) How to print first line/lines of the file
>>head -8 test.txt
Labels:
awk,
command line,
scripts,
shell scripts,
unix,
unix commands
How to print last two columns of a file - AWK
1) How to print last two columns of a file?
NF => prints the last line
>>cat test.txt
prabhath kota Jan
lakshmi muvvala Feb
PRABHATH KOTA Mar
LAKSHMI MUVVALA Apr
>>awk '{print $(NF-1),"\t",$NF}' test.txt #RIGHT
output:
kota Jan
muvvala Feb
KOTA Mar
MUVVALA Apr
>>awk '{print $NF-1,"\t",$NF}' test.txt #WRONG
2) How to print last two columns of a command?
>> ls
-rw-r----- 1 kotaprax users 203 Nov 22 12:51 a.txt
-rw-r----- 1 kotaprax users 282 Nov 23 10:18 b.txt
-rw-r----- 1 kotaprax users 17 Nov 16 15:25 c.txt
>>ls -l | awk '{print $(NF-1),"\t",$NF}'
output:
12:51 a.txt
10:18 b.txt
15:25 c.txt
3) How to print the last line/lines of the file
>>tail -f test.txt
>>tail -4 test.txt #Prints Last 4 lines
>>tail -1 test.txt #Prints Last line of the file
Labels:
awk,
command,
command line,
scripts,
unix,
unix commands
Awk Examples
1) To fetch selected columns from a command using AWK
e.g.,
>> ls -l
-rw-r----- 1 kotaprax users 203 Nov 22 12:51 new.txt
-rw-r----- 1 kotaprax users 100 Nov 22 12:51 new1.txt
-rw-r----- 1 kotaprax users 126 Nov 23 12:57 output.txt
-rw-r----- 1 kotaprax users 282 Nov 23 10:18 test.txt
-rw-r----- 1 kotaprax users 17 Nov 16 15:25 test1.txt
From the abvoe command, we are deriving some columns
>> ls -l | awk '{print $3 "\t" $8 "\t" $9}'
kotaprax 12:51 new.txt
kotaprax 12:51 new1.txt
kotaprax 12:57 output.txt
kotaprax 10:18 test.txt
kotaprax 15:25 test1.txt
2) How to print first column from the file using AWK
e.g.,
>> cat test.txt
prabhath kota
lakshmi muvvala
PRABHATH KOTA
LAKSHMI MUVVALA
>>awk '{print $1}' test.txt
(or)
>>awk < test.txt '{ print $1 }'
o/p:
>>cat test.txt
prabhath
lakshmi
PRABHATH
LAKSHMI
3) How to copy the files using AWK
>> ls
a.txt
b.txt
c.txt
It will copy file.txt to file.txt.new for all the txt files
>>ls *.txt | awk '{print "cp "$1" "$1".new"}' | sh
o/p:
>> ls
a.txt
b.txt
c.txt
a.txt.new
b.txt.new
c.txt.new
4) How to list only directories using awk
>>ls
a.txt
b.txt
c.txt
prabhath_test
o/p:
>> ls -l | grep '^d'
#Lists only directories, since directories start with
drwxr-x--- 2 kotaprax users 1024 Nov 23 13:57 prabhath_test
>>ls -l | grep -v '^d' #Lists only files but not directories
-rw-r----- 1 kotaprax users 203 Nov 22 12:51 a.txt
-rw-r----- 1 kotaprax users 282 Nov 23 10:18 b.txt
-rw-r----- 1 kotaprax users 17 Nov 16 15:25 c.txt
5) Remove files using AWK
>>ls
a.txt
b.txt
c.txt
prabhath_test
a.tt
b.tt
>> ls -l *.tt | awk '{print "rm -r "$9}' | sh
#"rm" is a command, in order to execute, "sh" is used
o/p:
>>ls
a.txt
b.txt
c.txt
prabhath_test
6) AWK - Difference between $0 and $1
>> echo "hello1 hello2 hello3" | awk ' {print $0} ' #$0 prints the whole
hello1 hello2 hello3
>> echo "hello1 hello2 hello3" | awk ' {print $3} ' #$1 prints 1st column
hello3
7) How to print 1st column of a file into another file ?
>>cat test.txt
prabhath kota
lakshmi muvvala
PRABHATH KOTA
LAKSHMI MUVVALA
>> awk '{print $1}' test.txt > output.txt
o/p:
>>cat test.txt
prabhath
lakshmi
PRABHATH
LAKSHMI
Unix Grep Examples
Grep
######
1) How to exclude lines in a grep
syntax: grep -v <pattern> <filename>
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -v "prabhath" test.txt
It will exclude the lines which contain "prabhath"
o/p:
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
2) How to exclude lines in a grep with ignore case
syntax: grep -iv <pattern> <filename>
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -iv "prabhath" test.txt
It will exclude the lines which contain "prabhath", "Prabhath", "PRABAHTAH" etc.,
o/p:
ramesh
vamsi
lakshmi muvvala
LAKSHMI MUVVALA
3) How to exclude lines which contain multiple patterns at one shot
Syntax: grep -v -e <pattern> -e <pattern> <filename>
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>>grep -v -e "prabhath" -e "lakshmi" test.txt
It will exclude the lines which contains the pattern "prabhath" & pattern "lakshmi"
o/p:
ramesh
vamsi
PRABHATH
LAKSHMI MUVVALA
>>grep -iv -e "prabhath" -e "lakshmi" test.txt
Please note "i" - ignore case
It will exclude the lines which contains the pattern "prabhath", "PRABHATH" & pattern "lakshmi", "LAKSHMI"
o/p:
ramesh
vamsi
4) Counting the number of matches using grep -c
Syntax: grep -c "pattern" filename
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>>grep -c "prabhath" test.txt
1
>>grep -ic "prabhath" test.txt
2
5) Counting the number of lines that does not match the pattern
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>>grep -vc prabhath test.txt
5
(or)
>>grep -v -c prabhath test.txt
5
6) Display N lines around match (Before & After)
Syntax: > grep -C 2 "Pattern" File
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -C 2 "lakshmi" test.txt
o/p:
ramesh
vamsi
lakshmi muvvala //Actual Match
PRABHATH
LAKSHMI MUVVALA
7) Display N lines Before Match
Syntax: > grep -B 2 "Pattern" File
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -B 2 "lakshmi" test.txt
o/p:
ramesh
vamsi
lakshmi muvvala //Actual Match
8) Display N lines After Match
Syntax: >> grep -A 2 "Pattern" File
e.g.,
>> cat test.txt
prabhath
ramesh
vamsi
lakshmi muvvala
PRABHATH
LAKSHMI MUVVALA
>> grep -A 2 "lakshmi" test.txt
o/p:
lakshmi muvvala //Actaul Match
PRABHATH
LAKSHMI MUVVALA
9) Checking for full words, not for sub-strings using grep -w
e.g.,
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA
Syntax: >> grep -w "Pattern" File
>>grep -iw "kota" test.txt
o/p:
prabhath kota
PRABHATH KOTA
>>grep -i "kota" test.txt
o/p:
prabhathkota
prabhath kota
PRABHATHKOTA
PRABHATH KOTA
10) How to get line numbers in grep
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA
>> grep -n 'PRABHATH' test.txt
o/p:
7 PRABHATH
8 PRABHATHKOTA
9 PRABHATH KOTA
>> grep -in 'PRABHATH' test.txt
o/p:
1 prabhath
2 prabhathkota
3 prabhath kota
7 PRABHATH
8 PRABHATHKOTA
9 PRABHATH KOTA
11) Grep Pipe Examples
1) Display cpu model name
#With Pipe
]# cat /proc/cpuinfo | grep -i 'Model'
#Without Pipe
]# grep -i 'Model' /proc/cpuinfo
2) grep dbname *.dbl | grep mysql
12) Some complex examples using Grep...
>> cat test.txt
prabhath
prabhathkota
prabhath kota
ramesh
vamsi
lakshmi muvvala
PRABHATH
PRABHATHKOTA
PRABHATH KOTA
LAKSHMI MUVVALA
>> grep -nC 2 'lakshmi' test.txt
Note: "-C 2" will grep for 2 lines above and 2 lines below
"-n" will give the line numbers
o/p:
4 ramesh
5 vamsi
6 lakshmi muvvala
7 PRABHATH
8 PRABHATHKOTA
13) Grep OR
1) grep 'pattern1\|pattern2' filename
$ cat employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy Manager Sales $6,000
$ grep 'Tech\|Sales' employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
2) grep -E 'pattern1|pattern2' filename
$ grep -E 'Tech|Sales' employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
3) egrep 'pattern1|pattern2' filename
$ egrep 'Tech|Sales' employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
500 Randy Manager Sales $6,000
14) Grep AND
grep -E 'pattern1.*pattern2' filename
grep -E 'pattern1.*pattern2|pattern2.*
$ grep -E 'Dev.*Tech' employee.txt
200 Jason Developer Technology $5,500
The following example will grep all the lines that contain both “Manager” and “Sales” in it (in any order).
$ grep -E 'Manager.*Sales|Sales.*
15) Grep NOT
grep -v 'pattern1' filename
$ grep -v Sales employee.txt
200 Jason Developer Technology $5,500
300 Raj Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
$ egrep 'Manager|Developer' employee.txt | grep -v Sales
200 Jason Developer Technology $5,500
400 Nisha Manager Marketing $9,500
16) Highlighting the search using GREP_OPTIONS
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
17) Show only the matched string
#] grep -o
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
Subscribe to:
Posts (Atom)