SED – Stream Editor
Used to display & editing data
Editing options are – Insertion/Updation/Deletion
2 Types of Operations
———————
– Lines Addressing
– Context Addressing
Line Addressing-
Command 1: Display line multiple times
sed '2p' file.txt
sed -n '3p' file.txt (specific line => -n)
sed -n '5p' file.txt
Command 2: Display last line[$]
sed '$p' file.txt (includes last line again along with original)
sed -n '$p' file.txt (Specific)
Command 3: Range of lines
sed -n '2,4p' file.txt
Command 4: Do not display specific lines
sed -n '2!p' file.txt
sed -n '2,4!p' file.txt
- do not display specific range of lines(!)
Context Addressing:
Command 1: Display lines having a specific word
sed -n '/Amit/p' file.txt
sed -n '/[Aa]mit/p' file.txt (Ignoring Case)
Command 2: Search lines and store in the file
sed -n '/[Aa]/p' file.txt
sed -n '/[Aa]/w result.txt' file.txt (Write to result.txt)
Command 3: Replace context from the file
sed 's/10000/1500/' file.txt
sed 's/hello/hi/' file.txt
Command 4: Replace multiple context in one command
sed -e 's/100/150/' -e 's/200/250/' file.txt
Command 5: Replace data by matching some condition
sed '/Sai/s/100/150/' file.txt
sed '/Msd/s/100/150/' file.txt [Check the match(Msd) and Substitute]
Command 6: Delete data
sed '/sai/d' file.txt
sed '/[Aa]mit/d' file.txt
Command 7: Add line to file
sed '1i welcome to 24tutorials' file.txt
sed '1i welcome to \n 24tutorials' file.txt [i- insert]