Changes between Version 2 and Version 3 of Csle2022/Agenda/linuxfilters
- Timestamp:
- Nov 29, 2022, 12:28:29 AM (2 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Csle2022/Agenda/linuxfilters
v2 v3 100 100 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. 101 101 102 `$sed 's/unix/linux/' geekfile.txt`102 `$sed 's/unix/linux/' test.txt` 103 103 104 104 2. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word “unix” with “linux” in a line. 105 105 106 `$sed 's/unix/linux/2' geekfile.txt`106 `$sed 's/unix/linux/2' test.txt` 107 107 108 108 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. 109 109 110 `$sed 's/unix/linux/g' geekfile.txt`110 `$sed 's/unix/linux/g' test.txt` 111 111 112 112 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. 113 113 114 `$sed 's/unix/linux/3g' geekfile.txt`114 `$sed 's/unix/linux/3g' test.txt` 115 115 116 116 5.Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis. 117 117 118 `$ echo "Welcome To The GeekStuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'`118 `$ echo "Welcome To The Test Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'` 119 119 120 120 6.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 121 121 122 `$sed '3 s/unix/linux/' geekfile.txt`122 `$sed '3 s/unix/linux/' tezt.txt` 123 123 124 124 7.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. 125 125 126 `$sed 's/unix/linux/p' geekfile.txt`126 `$sed 's/unix/linux/p' test.txt` 127 127 128 128 8.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. 129 129 130 `$sed -n 's/unix/linux/p' geekfile.txt`130 `$sed -n 's/unix/linux/p' test.txt` 131 131 132 132 9.Replacing string on a range of lines : You can specify a range of line numbers to the sed command for replacing a string. 133 133 134 `$sed '1,3 s/unix/linux/' geekfile.txt`134 `$sed '1,3 s/unix/linux/' test.txt` 135 135 136 136 10.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 … … 199 199 200 200 201 202 203 204 205 206 207 208