Linux & VIM command

 Basic commands


cp
mv
cat
less
more
tail
head
wc
chmod
pipes and redirection
basic permissions
Filename wildcards
grep -A n  ###     prints n lines before the specified word
grep -B n  ###     prints n lines after the specified word

Vim shortcuts

v for visual mode esc Normal mode for navigating through the documents : command mode i insert mode % across all lines s substitute /g global otherwise it will execute only once per line :n jump to line number n u undo ctrl +r redo H,M,L shifting cursor on page h, j ,k ,l cursor navigation shift + g shift to end of file gg shift to beginning of file Insert mode i change to insert mode at beginning of cursor position I change to insert mode at beginning of the line o change to insert mode with new line added below current line O change to insert mode with new line added above current line a change to insert mode at after the current cursor position. A change to insert mode with cursor at end of the line. cc clear the line of cursor position C clear the line after position of cursor Visual mode V Visual line In visual line mode it select the complete line of cursor v Visual mode In visual mode this works as the normal mode with selection of words and lines are done CTRL + v visual block In Visual block we can select the text in both vertically and horizontally as block and it can be more used for multiple line editing with same text inserting . ie ctrl + v G I <enter text> esc w select the word in visual mode yy yank or copy selected in visual mode y yank or copy selected in visual block p paste U convert selected text into Upper case u convert selected text into Lower case Searching Text * search for keyword of cursor placed in forward . # search for keyword of cursor placed in backward direction. /pattern - search for pattern in forward. ?pattern - search backward for pattern. \ vpattern - 'very magic' pattern: non-alphanumeric characters are interpreted as n - repeat search in same direction N - repeat search in opposite direction :%s/old/new/g - replace all old with new throughout file :%s/old/new/gc - replace all old with new throughout file with confirmations :%s/old/new/gic - replace all old with new throughout file with confirmations and case ignored :set hls :noh - remove highlighting of search matches Files with Tabs & Commmand Mode :tabnew :tabprevious :tabp :tabnext :tabn :w filename :e /dir/file.txt :cd /dir :tabfirst :tablast :tabmove 4 shifts to tab 4 :put=range(start,end) <writes a list of numbers from below cursor position> :put=range(start,end,jump) <writes a list of numbers from below cursor position with interval> :for i in range(1,10) | put = '192.168.0.'.i | endfor :g/^/m0 reverse all lines in the document :reg shows us all the current registers :oldfiles shows us files that we have used previously :browse oldfiles opens file in current document tabmove 3 Macros 1) qa - record macro as 'a' 2) q - stop recording macro 3) @a - run macro 'a' 4) @@ - rerun last run macro ## 100@ repeat macro 100 times :set nu To set line numbers :set ignorecase Case sensitivity to ignore for searching word matching :set smartcase :vs ~/.vimrc vimconfiguration for the file :source ~/.vimrc save's the configuration file :sp ...split the vim editor window and ctrl+w to shift between windows :sp file - open a file in a new buffer and split window 7) :vsp file - open a file in a new buffer and vertically split window 8) Ctrl + ww - switch windows 9) Ctrl + wq - quit a window 10) Ctrl + wv - split window vertically 11) Ctrl + wh - move cursor to the left window (vertical split) 12) Ctrl + wl - move cursor to the right window (vertical split) 13) Ctrl + wj - move cursor to the window below (horizontal split) 14) Ctrl + wk - move cursor to the window above (horizontal split) :vimdiff file1 file2 tkdiff file1 file2 Counting a word/pattern in file :%s/pattern//gn grep -c "string" file.txt
Deleting lines in Vim editor
Command mode   : 5d or d5d ,d delete 5 lines from cursor position
			 :n1,n2,d           delete  lines from 2 to 5 
			 :%d               delete all file content 
			 : .,$d            delete from current line to end of file
			 :g /word/d   delete lines contains with specific word
			 :g! /word/d delete lines which does not contain specific word
			 :g/^$/d        delete all empty lines of file.
Normal mode(esc) :  dgg              delete from current line to starting of file
	                           o or O          to open a new line below or above  cursor position.
			
			
			
			dw    --> delete the word
			.        -->repeates the previous operation
			
		
			



ctrl +L    start writing from top of screen
Ctrl+A   jump to beginning of command
ctrl+E    jump to end of command
ctrl+W   deleting entire line of command
ctrl+c       force stop a process

GREP {Global regular expression print}
 
cat    filename
cat filename.txt  | grep "hello"  --color



Text substitution  and transformation  --s is substitution   --y is for transformation
  --e  for extended   --
 
Deleminators  can be '.'  'Space'  '/'   
sed 's/patter/replacing word/'  file.txt > newfile.txt
#save the modified in newfile
's/G/@/ig'
#ignores case sensitive
's/^.*on/REPLACED/'
#from beginning of line to *on matching word
's/^.*on/(&)/'
# keeping paranthesis for the matched word
sed  -  i  's/patter/replacing word/'  file.txt            
#replacing and saves by over writing
sed  -  i  's.patter.replacing.'  file.txt     
#using . as delimiter
Sed  - i  's./etc..' file.txt
#removing /etc from entire file
'P'
Print each line twice
-n 'P'
Supresses automatic printing of modified
-n '1P'
Prints only first line
-n '3,5P'
prints  lines from 3 to 5
-n '1~2P'
 interval after ~ character. Print every other line starting with line 1.
'1~2d'
deleting the lines in alternating 
'1,3s/.*/Hello/'
for selected line numbers
'/John Adams/s/.*/Hello/' 
for matched word in line of file
'/^$/d'
for removing blank lines
 
 


 
-$echo  hi shifa
>>    hi shifa
-$ echo hi shifa | sed 's/hi/bye'
>>   bye shifa
-$ echo hi shifa | sed 's/hi/bye/g'
>>   bye  sbyefa 
-$ cat filename.txt  | sed '/saba/s/hi/bye'  #find and substitute line with "saba" string

-$cat filename.txt  | sed 's/hi/bye/'; 's/hello/jello/g'    #multiple substitutions
 
-$ cat file.txt | sed 's/^/https:\/\/ /'            # multiple line substitution at beginning of each line
-$ cat file.txt | sed 's/^$/d'                            #removing all empty lines in file  
-$ cat file.txt | sed 's/[0-9]//g'                       ##removing all  lines with numers contained in it  
-$ cat file.txt | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/',
#Transforming lower case to upper case
-$ cat file.txt | sed -e 's/com/abc/'  -e 's/in/mno/'   #extending to multiple expressions
 



 
awk {BEGIN  BLOCK} {ACTION BLOCK }{END BLOCK}
awk 'BEGIN{print " Title Bar"}  {print"print every line"}END {print"Thank you, end of the file}'   file.txt
Processing Tabular Data with AWK:-
$0 -->represent the each line
$1 -->represents the first field in current line
$2 -->represents the second field in current line
etc... FFJKDGH
 
awk '{print $1}'  file.txt
awk '{print $1"----->"$5}' file.txt
awk '{print $0}'  file.txt                                                   # prints all columns data
 
By default tab is used as data seperator  , but for other files like csv, and other formats we use  -F or FS variable 
 
awk  -F ","  '{print $2"  ===>   "$4}' file.csv                 # field seperator used in valid formats
awk '{print $2"   "$4}' FS=","   file.csv
 
awk '{print $4 , $11, $13, $14, $15}'  file.txt | tee report.txt
 
awk 'BEGIN{printf "Sr No\tName\tSub\tMarks\n"} {print}' marks.txt
awk -v name=Jerry 'BEGIN{printf "Name = %s\n", name}'
awk '{print $3 "\t" $4}' marks.txt
awk '/a/ {print $0}' marks.txt
awk '/a/ {print $3 "\t" $4}' marks.txt
awk '/a/{++cnt} END {print "Count = ", cnt}' marks.txt
 
 Soft link and hard link
Soft link is like creating shortcut.  Hard link is like creating copy of the file..
 
 
-$ cat file.txt
 hi shifa
hello shifa
hi saba
hi xyz
hello qwerty
 
-$ cat file.txt |awk '{print $1}'                                        #prints all the first column elements in file
-$ cat file.txt | awk '{print $2}'                                       #prints all the second column elements in file
-$ cat file.txt | awk '/shifa/{print $2}'                           #search for "shifa" in all lines and print the second column  elements
-$ awk  -F  '.'  ' {print $1} '  file.txt                                  #prints all the first column elements seperated by  '.'
-$ awk  '{s+=$1} END { print s}' file.txt                           #prints the sum of first column elements

Comments