UNIX Sed, Vi and Awk Command Examples
UNIX Sed, Vi and Awk Command Examples
UNIX Sed, Vi and Awk Command Examples
To search and replace, use the sed 's' action, which comes in front of two
expressions:
cat filename | sed 's/string_old/string_new/' > newfile
Substitute Only 2nd Occurrence of a Word Using sed s//2
$ sed 's/Linux/Linux-Unix/2' thegeekstuff.txt
s/99/-9999\.00/g
s/Basin[0-9]//g
s/Basin$//
/^$/
ab|cd Either ab or cd
append
c
d
i
p
s
n
q
change lines
delete lines
insert
print lines
substitute
display lines
Quit lines
Delete Starting from 3rd line and every 2nd line from there.
Sed 3~2d <FileName>
Here in this command it starts deleting from 3rd line from then every 2nd line till end of the
file.
Deleting from 4th to 8th line in a file.
Sed 4,8d <FileName>
Delete line which Matched pattern.
Sed /done/d <filename>
Sed /done/,$d <FileName> here it will delete wherever done matches till last line.
Sed /done/,+2d <FileName> here it will delete wherever done matches from there next
2lines.
Delete lines other than the specified range
Sed 2,4!d <FileName>
Delete first and last line
Sed 1d;$d <FileName>
Delete lines that begin with particular character
Sed /$a/d <FileName>
Sed /^a/d <FileName> Deletes line which starts with letter a.
Sed /$a/d <FileName> Deletes line which ends with letter a.
Undo and Change Commands Command Meaning
cw
Change word (or part of word) at the cursor location to the
end of the word
3cw
Change three words
r
Replace character at cursor with one other character
u
Undo previous command
Basic Search Commands
G (upper case)
:21
/string
?string
n
Command Meaning
Go to last line of file
Go to line 21
Search forward for string
Search backward for string
Find next occurrence of string
Split the file into multiple pieces based up on the number of lines using -l option as shown
below.
Syntax: split l(lines) <rooflines> <Actual Filenames> <AfterSpltFileName>
-d Option :
Use -d option to name the files with number suffixes as 00, 01, 02 .. and so on, instead of
aa, ab, ac.
Syntax : $ split -d testfile
$ ls -ltr
testfile x00 x01 x02
How to Find the length of a File Name
A=`expr length <FileName>
Echo $A
How to find length of each line in a file
Awk {print length} <FileName>
How to set numbers in Vi Editor
:set nu Or :set number
How to remove line numbers in Vi Editor
:set nu! Or :set nonu Or :set nonumber
How to get Previous date in Unix
Date +%Y-%m-%d
date +%Y-%m-%d date - 1 day
date +%Y-%m-%d --date "- 1 month"
date +%Y-%m-%d --date "- 1 year"
Common Functionalities of SED and AWK Command :
1 ). We can Find Total no of record count
Sed : sed -n '$=' <File Name>
Awk : cat Archive_Script.SH|awk '{print NR}'|tail -1
2). We can get exact Records in a file
Sed : sed n 5p <File Name>
Awk : cat Archive_Script.SH|awk 'NR==5
How to get only Zero Byte files which are present in the directory
Awk :
Ls ltr | awk /^-/ { if($5 ==0) print $9 }
Find :
find -type f -size 0
How to Find the length of each file in a directory:
ls | awk '$(NF+1)=length'
How add a First record and Last Record to the current file in Linux
sed -i -e '1i Header' -e '$a Trailor' Success.txt
another way
sed -i -e '1i Header' Success.txt
Echo Trailor>>Success.txt(It will append at end of a line).
How to display Even number of records into one file and Odd number of records
into another file
Awk NR % 2 == 0 <File Name > >Even
Awk NR % 2 == 0 <File Name > >Odd
At end records will load into even and odd files.
Awk String Functions:
Length: echo "Janakiram Dabbara"|awk '{print length}'
Substr: echo "Janakiram Dabbara"|awk '{print Substr ($0,1,9)}'
Toupper: echo "Janakiram Dabbara"|awk '{print toupper ($0)}'
Tolower : echo "Janakiram Dabbara"|awk '{print tolower($0)}'
!Sed
Wc
!Wc
Grep
!grep
Awk
!awk
Find
!find
Head