Changes

Jump to: navigation, search

Tutorial11: Sed & Awk Utilities

63 bytes added, 11:15, 18 February 2021
INVESTIGATION 1: USING THE SED UTILITY
# Issue the '''more''' command to quickly view the contents of the '''data.txt''' file.<br>When finished, exit the more command by pressing the letter <span style="color:blue;font-weight:bold;font-family:courier;">q</span><br><br>
# The '''p''' command in sed is used to print or display the contents of a text file.<br>Issue the following linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed 'p' data.txt</span><br><br>You should notice that each line appears '''twice'''. The reason why standard output appears twice is that the sed command (without the '''-n option''') displays all lines regardless if they had been specified as a pattern.<br><br>
# Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n 'p' data.txt | tee sed-1.txt</span><br><br>What do you notice?<br><br>You can specify an address (line #, line #s or range of line #s) when using the sed utility.<br><br># Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '1 p' data.txt | tee sed-2.txt</span><br><br>You should see the first line of the text file displayed.<br><br># Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '2,5 p' data.txt | tee sed-3.txt</span><br><br>What is displayed? How do you change command to display lines 2 to 5?<br><br>The '''s''' command is used to substitute patterns (similar to method demonstratedin vi editor).<br><br># Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed '2,5 s/TUTORIAL/LESSON/g' data.txt | tee sed-4.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 '''quits''' the execution of the sed utility as soon as it read in a particular line or matching pattern.<br><br># Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed '11 q' data.txt | tee sed-5.txt</span><br><br>What did you notice?<br><br>You can use regular expressions to select lines that match a pattern. The rules remain the same for using regular expressions as demonstrated in lab8 except the regular expression must be contained within delimiters such as the forward slash "/" when using the sed utility.<br><br># Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '/^The/ p' data.txt | tee sed-6.txt</span><br><br>What do you notice?<br><br># Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">sed -n '/d$/ p' data.txt | tee sed-7.txt</span><br><br>What do you notice?<br><br>The '''sed''' utility can also be used as a filter to manipulate text that was generated from linux commands.<br><br>
# Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls | sed -n '/txt$/ p' | tee sed-8.txt</span><br><br>What did you notice?<br><br>
# Issue the following linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">who | sed -n '/^[a-m]/ p' | tee sed-9.txt | more</span><br><br>What did you notice?<br><br>
13,420
edits

Navigation menu