You are on page 1of 7

UNIX Sed Command Options

1).sed -n '$=' myfile.txt


Above this command count the number of lines in the myfile.txt and output the results.
2).Write first & last line of the file
In this example, 1 and $ refers first and last line respectively.
$ sed -n -e '1w output.txt' -e '$w output.txt' >thegeekstuff.txt
3).Write the lines matches with the pattern Storage or Sysadmin
In this example sed command writes the lines which matches the pattern Storage or
Sysadmin.
$ sed -n -e '/Storage/w output.txt' -e '/Sysadmin/w output.txt' thegeekstuff.txt
4).Write the lines from which the pattern matches to till end of the file
In this example, /Storage/,$ represents line matches from Storage to end of the file.
$ sed -n '/Storage/,$w output.txt' thegeekstuff.txt
5).Write the lines which matches pattern and next two lines from match
In this example, the send command writes the line matches for Storage and two lines next
to that.
$ sed -n '/Storage/,+2w output.txt' thegeekstuff.txt
6). Write 1st line of the file
In this example, 1 (address) refers the first line of the input and
w writes the pattern buffer to the output file output.txt
$ sed -n '1w output.txt' thegeekstuff.txt
How to Display 1st and 3rd line in a File
Sed n 1p;3p <file name>
7).Delete 4th and 2nd line from the input
This sed example deletes 4th and 2nd line from the file thegeekstuff.txt. Using -e option,
you can give any number of commands with sed.
$ sed -e '4d' -e '2d' thegeekstuff.txt
8). Delete the first, last and all the blank lines from input
This sed example deletes the first line, last line and all the blank lines from input file.And
redirect to file Johnny.txt
$ sed -e '1d' -e '$d' -e '/^$/d' thegeekstuff.txt >Johnny.txt
9).sed -e 's/linux/unix/' -e 's/after/before/'
10).To remove all of the lines containing "two" from the myfile.txt file:

$ sed '/two/ d' myfile.txt


11).To remove the first three lines from the display, regardless of what they are:
$ sed '1,3 d' myfile.txt
12).To delete all lines that contain a 'Windows' word, enter:
$ sed '/Windows/d' /tmp/data.txt > /tmp/output.data.txt
13).To delete the lines 2 through 4 in the above file, use the following sed
command
$ sed '2,4d' textfile.txt
14).Delete all occurrences of a pattern in a file using sed command
This is a simple application of the /s flag where the target string is none.
For example, to remove all occurrences of word "line" in the above file, the sed command
would be
$ sed 's/line//' textfile.txt
This is 1T
his is 2
This is 3
This is 4
This is 5
15).To remove all occurrences of the character space in the above input file,
the sed command would be
bash-3.00# sed 's/ //g' textfile.txt
Thisisline1
Thisisline2
Thisisline3
Thisisline4
Thisisline5
16).Deleting lines which matches a pattern using sed command
To delete lines which matches a pattern, use the /d flag along with the
pattern to be matched, for example, to delete lines matching the pattern "line 1" in the
above file, the command would be
sed '/line 1/d' textfile.txt
17).Deleting the first line which matches a pattern using sed command
sed '/line/{1d;}' textfile.txt
18).Deleting the last line which matches a pattern using sed command
sed '/line/{$d;}' textfile.txt

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

search for all '99' replace with '-9999.00'

s/Basin[0-9]//g
s/Basin$//

remove the word Basin followed by a single digit


remove the word Basin if it is at the end of
the line.

sed commands are usually on one line


if we want more (multi-line commands), then we must end the first line with an `\'
if a command is one line only, it can be separated by a `;
if it is a multi-line, then it must contain all of its line (except the first) by themselves
on command line, what follows a `-e' is like a whole line in a sed script
/..*/

At least 1 character long (/.+/ means the same thing)

/^$/

the empty line

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 comments: sed -e '/^#/d


Remove all empty lines:
sed '/^$/d' filename.txt
sed '/./!d' filename.txt
sed -i" overwrites the original file with a new one

How to Print only Blank Line of File.


sed -n '/^$/p' <File Name>
Command to Delete Commented Lines and Blank Lies
sed -e 's/#.*//' -e '/^$/ d'
To delete only the word use:
sed 's/yourword//g' yourfile
To delete two words from a file simultaneously use:
sed -e 's/firstword//g' -e 's/secondword//g' yourfile
Pipeline
Pipeline lets programs use the output of a program as the input of another one with the
symbol: |
Take the previous example:
cat file1 | sed s/Hello/Word/ > file2
myprogram.exe file2 > file3
To Print First and Last Line using Sed Command
Sed -n 1 p <File_Name>
Sed n $ p <File_name>
To Print all line Except First Line
Sed n 1! p <File_Name>
Delete all Line except First Line
Sed n 1! d <file_name>
Delete all Line which matches a word
sed i /a/d <filename>
Command to find 3rd Column, 4th Row in that First 4chars:
Awk F {print $3}|awk NR>1|sed n 4p|cut c 1-4

Vi Editor (Find and Replace)


:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
:1,10s/hello/Hello/g

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 Command in Linux


-l Option:
Using this command we can split the records into multiple number of files.

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)}'

To see last executed commands in Linux:


Sed

!Sed

Wc

!Wc

Grep

!grep

Awk

!awk

Find

!find

Head

!Head and so on for some more commands.

You might also like