Sed or Awk Linux
Sed or Awk Linux
sed Command
SED command stands for stream editor and it can perform lots of function on file like,
searching, find and replace, insertion or deletion. Though most common use of SED
command in UNIX is for substitution or for find and replace. By using SED you can
edit files even without opening it, which is much quicker way to find and replace
something in file, than first opening that file in VI Editor and then changing it.
• SED is a powerful text stream editor. Can do insertion, deletion, search and
replace (substitution).
• SED command supports regular expression which allows it perform complex
pattern matching.
Syntax:
• sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
# cat >file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is
a powerful.
Sample Commands
1. Replacing or substituting string : Sed command is mostly used to replace
the text in a file. The below simple sed command replaces the word “unix” with
“linux” in the file.
1
FYCS | SEM II UNIT 3 LINUX
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a
powerful.
3. Replacing all the occurrence of the pattern in a line : The substitute flag /g
(global replacement) specifies the sed command to replace all the
occurrences of the string in the line.
# sed 's/unix/linux/g' file.txt
Output :
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a
powerful.
4. Replacing from nth occurrence to all occurrences in a line : Use the
combination of /1, /2 etc and /g to replace all the patterns from the nth
occurrence of a pattern in a line. The following sed command replaces the
third, fourth, fifth… “unix” word with “linux” word in a line.
# sed 's/unix/linux/3g' file.txt
Output:
unix is great os. unix is opensource. linux is free os.
learn operating system.
Unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a
powerful.
5. Replacing string on a specific line number : You can restrict the sed
command to replace the string on a specific line number. An example is
# sed '3 s/unix/linux/' file.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful.
6. Duplicating the replaced line with /p flag : The /p print flag prints the
replaced line twice on the terminal. If a line does not have the search pattern
and is not replaced, then the /p prints that line only once.
# sed 's/unix/linux/p' file.txt
2
FYCS | SEM II UNIT 3 LINUX
Output:
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn. unix is a multiuser os.Learn unix .unix is a
powerful.
linux is easy to learn. unix is a multiuser os.Learn unix .unix is a
powerful.
7. Printing only the replaced lines : Use the -n option along with the /p print
flag to display only the replaced lines. Here the -n option suppresses the
duplicate rows generated by the /p flag and prints the replaced lines only one
time.
# sed -n 's/unix/linux/p' file.txt
Output:
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn. unix is a multiuser os.Learn unix .unix is a
powerful.
8. Replacing string on a range of lines : You can specify a range of line
numbers to the sed command for replacing a string. -- > line no 1 to line no 3
# sed '1,3 s/unix/linux/' file.txt
Output:
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful.
Here the sed command replaces the lines with range from 1 to 3.
# sed '2,$ s/unix/linux/' file.txt → second line to last line
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful
3
FYCS | SEM II UNIT 3 LINUX
Here $ indicates the last line in the file. So the sed command replaces the text from
second line to last line in the file.
9. Deleting lines from a particular file : SED command can also be used for
deleting lines from a particular file. SED command is used for performing
deletion operation without even opening the file
Examples:
1. To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt n → line number
Example:
$ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
3. To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt -- > line no 3 to line no 6
5. To Delete from nth to last line
Syntax:
$ sed 'n,$d' filename.txt
Example:
$ sed '12,$d' filename.txt → line no 12 to last line
6. To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt
4
FYCS | SEM II UNIT 3 LINUX
AWK command
AWK Stands for ‘Aho, Weinberger, and Kernighan‘
Awk is a scripting language which is used for processing or analyzing text files. Or we
can say that awk is mainly used for grouping of data based on either a column or field ,
or on a set of columns. Mainly it’s used for reporting data in a usefull manner. It also
employs Begin and End Blocks to process the data.
Example:
Consider the following text file as the input file for all cases below.
$cat > employee.txt
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
1. Default behavior of Awk : By default Awk prints every line of data from the
specified file.
# awk '{print}' employee.txt
Output:
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000
5
FYCS | SEM II UNIT 3 LINUX