0% found this document useful (0 votes)
99 views

Sed Awk Commands

This document provides examples of using sed and awk commands to manipulate text files. Some examples include replacing or substituting text, running multiple sed commands, deleting lines within a range, and printing specific fields. Sed can be used to replace, insert, delete, or print lines matching patterns. Awk can print fields, filter lines, and perform other text manipulations.

Uploaded by

Konda Pavankumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Sed Awk Commands

This document provides examples of using sed and awk commands to manipulate text files. Some examples include replacing or substituting text, running multiple sed commands, deleting lines within a range, and printing specific fields. Sed can be used to replace, insert, delete, or print lines matching patterns. Awk can print fields, filter lines, and perform other text manipulations.

Uploaded by

Konda Pavankumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Examples

1. Replacing or substituting string or all occurences


sed 's/unix/linux/' file.txt or sed 's/unix/linux/g' file.txt
2. Replacing the nth occurrence of a pattern in a line or all occurences
sed 's/unix/linux/2' file.txt or sed 's/unix/linux/3g' file.txt
3. Changing the slash (/) delimiter
sed 's/http:\/\//www/' file.txt
sed 's_http://_www_' file.txt
sed 's|http://|www|' file.txt
4. Using & as the matched string
sed 's/unix/{&}/' file.txt sed 's/unix/{&&}/' file.txt
5. Using \1,\2 and so on to \9
sed 's/\(unix\)/\1\1/' file.txt
sed 's/\(unix\)\(linux\)/\2\1/' file.txt
6. Duplicating the replaced line with /p flag
sed 's/unix/linux/p' file.txt
7. Printing only the replaced lines
sed -n 's/unix/linux/p' file.txt
8. Running multiple sed commands.
sed -e 's/unix/linux/' -e 's/os/system/' file.txt
9. Replacing string on a range of lines.
sed '1,3 s/unix/linux/' file.txt
10. Replace on a lines which matches a pattern.
sed '/linux/ s/unix/centos/' file.txt
11. Deleting lines.
sed '2 d' file.txt
sed '5,$ d' file.txt
12. Add a line after a match.
sed '/unix/ a "Add a new line"' file.txt
13. Add a line before a match
sed '/unix/ i "Add a new line"' file.txt
14. Change a line
sed '/unix/ c "Change line"' file.txt
15. Delete both leading and trailing white spaces from each line
sed 's/^[ \t]*//;s/[ \t]*$//‘ file.txt
……..
sed -e '1,10d' /etc/services | more
sed -e '1,10d' /etc/services | more
sed -n -e '/BEGIN/,/END/p' file.txt
AWK Commands

awk '{ print $NF }' print the last field of each line
awk '{print NR "\t" $0}' file.txt precede each line by its line number FOR ALL FILES
ls -lrt | awk '{ print NF ":" $0 } ' print the no of fields of each line
ls -lrt | awk 'NF > 4' print the lines whose fields are more than 4
awk 'NF > 4' print every line where the value of the last field is > 4
# IN UNIX ENVIRONMENT: convert DOS newlines (CR
awk '{sub(/\r$/,"")};1' format
awk '{sub(/$/,"\r")};1' # IN UNIX ENVIRONMENT: convert Unix newlines (LF)
awk '{gsub(/^[ \t]+|[ \t]+$/,"")};1' delete BOTH leading and trailing whitespace from each
awk -F ":" '{print $1 | "sort" }'
/etc/passwd print and sort the login names of all users
awk 'NR < 11' print first 10 lines of file (emulates behavior of "head")
awk 'NR>1{exit};1' print first line of file (emulates "head -1")
awk 'END{print}' print the last line of a file (emulates "tail -1")
awk '!($5 == "abc123")' print lines which have less than 5 fields

You might also like