Open main menu

CDOT Wiki β

Changes

Tutorial11: Sed & Awk Utilities

4 bytes added, 09:28, 24 July 2020
INVESTIGATION 1: USING THE SED UTILITY
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '1 p' data.txt</span><br><br>You should see the first line of the text file displayed.<br><br>
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '2,5 p' data.txt</span><br><br>What do you think is displayed? (in another SSH session, compare with contents in the data.txt text file to confirm).<br><br>The '''s''' command is used to substitute patterns (similar to method demonstratedin vi editor).<br><br>Usually and address of lines or regular expression is used to define matching lines and then the "s" command is used followed by commands to either display standard output or write standard output to a file.<br><br>
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed '2,5 s/TUTORIAL/LESSON/g' data.txt</span><br><br>What do you notice? View the original contents of lines 2 to 5 in the '''data.txt''' file in another shell to confirm that the substitution occurred.<br><br>The '''q''' command terminates or quitsthe execution of the sed utility as soon as it read in a particular line or matching pattern.<br><br>
# Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed '11 q' data.txt</span><br><br>
# x
13,420
edits