Unix Commands
Unix Commands
UNIX COMMANDS
-----------------------------------
------------------------
working with directories
------------------------
--------------------
working with files
--------------------
#escape mode
15.to paste
p
10p #paste 10 times
Syntax Pattern
a*bc abc aabc aaaaabc
a.c aac abc azc
abc .* xyz
a[b2m]c abc a2c amc
a[0-2b-d]c a0c a1c a2c ---- abc acc adc
a[^0-2]c a3c a4c axc
a\{3,\} 3 or more than 3 a
a\{3,5\} between 3 to 5 a's
:.,$s/abc/efg/g #
subtitute abc with efg from current line to last line
:.,$s/ab./efg/g
:.,$s/ab[a-z]/efg/g
:g/set/s/black/white/g # where
set is found replace black with white
:g/set/!s/black/white/g
:%s/\(.*param.*=.*\) \(MIN.*\)/\1\ '\2\'/g
:/hello/!s/aaa/bb/ # unless
'hello' is found, replace 'aaa' with 'bb'
:g/MFPT1\/P1_[0-9]*/s/W=[0-9]*\.[0-9]* /W=2\.025 /g
:g/MFPT1\/P1_[0-9]*/s/W=[0-9]*\.[0-9]* /W=2\.025 /g # search
pattern MFPT1/P1..numbers ,replace W=..number to W=2
:g/MFPT1\/P1_[0-9]*/s/\<W=[0-9]*\.[0-9]*\> /W=2 /g
:%s/w=\([0-9]*\.[0-9]*\)U /W=\1 /g # search
and replace w=something with pattern1 (patern inside small braces)
:%s/\(.*param.*=.*\) \(MIN.*\)/\1\ '\2\'/g
:%s/w=\([0-9]*\.[0-9]*\). /W=\1 /g #to
reduce the accuracy of decimal points
:%s/W=\<[0-9]*.[0-9]*\>/&U/g #where W= is
found add U in the end
:g/hello/,/goodbye/d #delete all
lines between hello and goodbye
:g/^ *$/d #deletes all
lines which is blank or tab
:g/^ *$/di #deletes all
lines which is blank or tab
:23,/abd/d #from line 23
to first match of abd will get deleted
:g/FPT.\/P.IO.*DATFLOP\/QF_X/s/^/#/g #FPT3/P2IO[30]/
DATFLOP/ISCAN2/SC2B FPT3/P2IO[30]/DATFLOP/QF_X
:g/C_EST/s/ [1-9]*.[0-9]*f $/ 1f/g
:g/ISTCNTL/s/w=0.7/w=0.32/g
:g/xistcntl/s/w=0.7/w=0.32/g
:g/vss vdd vss vss/d #searches these
pattern together and delets the line
21.setting
:se nu #sets the number on lines
:se syn off
:w <File>
:x,y w <File>
:x,y d
:r <file>
:set wrap!
:set ignorecase #Ignore case in searches
:set smartcase #Ignore case in searches excepted if an
uppercase letter is used
:%s/\<./\u&/g #Sets first letter of each word to uppercase
:%s/\<./\l&/g #Sets first letter of each word to lowercase
:%s/.*/\u& #Sets first letter of each line to uppercase
:%s/.*/\l& #Sets first letter of each line to lowercase
22.sorting
23.selecting
ctl+ v then release key and select column and row portion #Select
column
shift+ v then release key and selecting row using cursor #Row Select
24.Read/Write files
25.file explorer
:e . #Open integrated file explorer
:Sex #Split window and open integrated file
explorer
:browse e #Graphical file explorer
:ls #List buffers
:args #List files
:args *.php #Open file list
:grep expression *.php #Returns a list of .php files contening
expression
:gf #Open file name under cursor
26.Alignment
:%!fmt #Align all lines
:!}fmt #Align all lines at the current position
:!!fmt #Align the next 5 lines
27.Tabs
:tabnew #Creates a new tab
:gt #Show next tab
:tabfirst #Show first tab
:tablast #Show last tab
:tabm n(position) #Rearrange tabs
:tabdo %s/foo/bar/g #Execute a command in all tabs
:tab ball #Puts all open files in tabs
28.Window spliting
:sp #Split window above
:vs #Split window side
:e filename #Edit filename in current window
:split filename #Split the window and open filename
ctrl-w up arrow #Puts cursor in top window
ctrl-w ctrl-w #Puts cursor in next window
ctrl-w_ #Maximise current window
ctrl-w= #Gives the same size to all windows
10 ctrl-w+ #Add 10 lines to current window
:vsplit file #Split window vertically
:sview file #Same as :split in readonly mode
:hide #Close current window
:only #Close all windows, excepted current
:b 2 #Open #2 in this window
30.Auto-completion
Ctrl+n Ctrl+p #(insert mode) Complete word
Ctrl+x Ctrl+l #Complete line
:set dictionary=dict #Define dict as a dictionnary
Ctrl+x Ctrl+k #Complete with dictionnary
31.Marks
mk #Marks current position as k
k #Moves cursor to mark k
dk #Delete all until mark k
32.Abbreviations
:ab pr printf("This is a Demo Ver \n"); Define pr as abbreviation of
printf("This is a Demo Ver \n");
33.Text indent
:set autoindent #Turn on auto-indent
:set smartindent #Turn on intelligent auto-indent
:set shiftwidth=4 #Defines 4 spaces as indent size
ctrl-t, ctrl-d #Indent/un-indent in insert mode
>> #Indent
<< #Un-indent
>/<
34.Syntax highlighting
:syntax on #Turn on syntax highlighting
:syntax off #Turn off syntax highlighting
:set syntax=perl #Force syntax highlighting
35.Editing a File
#.............................................................gvim..............
..........................................
#copy the below commands also inside your ~/.vimrc file, this will be useful
such that while using gf the file will be opened in new tab.
a. set autolist
---------
36.sed
---------
sed -e'/MFPT1\/P1_[0-9]*/s/\<W=[0-9]*\.[0-9]*\> /W=2 /g'
#search MFPT1/P1..something replace W=something with W=2
sed '/Owner:/{s/yours/mine/g;s/your/my/g;s/you/me/g;}' file
sed -e '17,/foo/d'
#delete all lines from line 17 up to 'foo'
echo 'C123 node1 node2 12ff $X=123 $Y=234' | sed -e 's/^C\(.*\) \([0-
9]*\)ff /C\1 {\2ff*mpar} /g'
echo 'C123 node1 node2 12e-15 $X=123 $Y=234' | sed -e 's/^C\(.*\) \([0-
9e-]\{1,\}\) /C\1 {\2*mpar} /g'
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-
---------
38.awk
---------
awk '{print $4}' file
#printing fourth feild
awk '{print $1,$2,$4}' file
#printing multiple feild
awk 'NR==3 {print $1,$2}' file
#prints Ist and IInd field of third row
awk 'NR==3,NR==6 {print $1,$2}' file
#from third row to sixth row
awk '{printf("%-30s %-20f %-10f\n", $1, $2, $3)}' File
#formatting the o/p
awk '{print s +=$4}' File
#cumulatively adding field 4
awk '/MAX_RD/ {print $0}' File
#Search Pattern then take action
awk '{if ($2 !=0 )printf("%2.2f%\n",($1-$2)/$2*100); else printf("0.0\
n")}' file #comparing then calculating percentage
awk '{for(i=1;i<=13;i++){printf("%-14s ",$i)};printf "\n"}'
#for loop inside awk command
awk '{ if ($5 == "PER" ) {printf ("%-1s %-1s \n",$1,$2) } else {print $0}}'
file #comparing field 5 and then taking action
awk '{ if (/^MP|C/) {print $1,$2,$3 }}' file
#prints lines begining with MP or C
awk '{ if ($2~/^ST/) {printf ("%s %s %s 1f\n",$1,$2,$3) } }' file
#prints lines having ST in IInd field
awk '{ if ($2~/ST/&& $6~/pmos/) {print $1,$2,$3} }' file
#prints lines having ST in IInd field and pmos in VIth field
awk '{split($7,arr,"="); print arr[2]}' file
#splits field 7 on delimiter '=' and stores in array
'arr'
...........................................................................
......................................................
---------
39.grep
---------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------
------------------------------------
40.cat sed grep awk piped together
-----------------------------------
#piping | means
sending output of one command as input to other command
#.......................................................misclaneous.............
..............................................................................#
PID variable = $$
Status varible = $#
42.setting variable
@ abc =15 #setting variable for
numbers
set abc='hi how are you' #setting variable for
string
set outFile='/home/zia140/kalim/bitcellDC/snm/outFile'
cd $outFile
43.calling variables
$abc
44.printing
echo hi how are you
echo '$abc'
printf hi how are you
--------------------------------------------------------------------------------
--------------------------
45.while loop
@ i =1
while ($i <10)
echo $i
@ i +=1
end
45.a
45.b
while (($block_2d[$i][0]=~ /^X_/) or ($block_2d[$i][0]=~ /^M/)){$i+=1;}
46.c
until ($netlist[$i]=~/WEN/){
$i+=1;}
--------------------------------------------------------------------------------
-------------------------------
46.if
46.a
if ( -f $name ) then #if file exists then print found
echo found
endif
46.b
if ( -e $name ) then #if file/directory exists then
take action
46.c
if (! -e $name ) then #if file/directory dosen't exists
then take action
46.d.if else
--------------------------------------------------------------------------------
-----------------------------
47.foreach loop
end
47.a.
foreach FoldLine (`cat ${Tran}.line`)
end
47.b.
foreach abc (`ls *spf*`)
sed -e "s/\#[A-Za-z0-9]* / /g" -e "s/^R/\*\*R/g" -e "s/^r/\*\*r/g"
$abc >m
\rm $abc
mv m $abc
end
--------------------------------------------------------------------------------
--------------------------
48.shell commond in perl
--------------------------------------------------------------------------------
--------------------------
49.Inserting a fileawk '{split($7,arr,"="); print arr[2]}'
#............................................................Sed/
vi ......................................................