| 283 | SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor. |
| 284 | |
| 285 | - SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution). |
| 286 | - SED command in unix supports regular expression which allows it perform complex pattern matching. |
| 287 | |
| 288 | 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. |
| 289 | |
| 290 | `$sed 's/unix/linux/' geekfile.txt` |
| 291 | |
| 292 | 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. |
| 293 | |
| 294 | `$sed 's/unix/linux/2' geekfile.txt` |
| 295 | |
| 296 | 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. |
| 297 | |
| 298 | `$sed 's/unix/linux/g' geekfile.txt` |
| 299 | |
| 300 | 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. |
| 301 | |
| 302 | `$sed 's/unix/linux/3g' geekfile.txt` |
| 303 | |
| 304 | 5.Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis. |
| 305 | |
| 306 | `$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'` |
| 307 | |
| 308 | 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 |
| 309 | |
| 310 | `$sed '3 s/unix/linux/' geekfile.txt` |
| 311 | |
| 312 | 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. |
| 313 | |
| 314 | `$sed 's/unix/linux/p' geekfile.txt` |
| 315 | |
| 316 | 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. |
| 317 | |
| 318 | `$sed -n 's/unix/linux/p' geekfile.txt` |
| 319 | |
| 320 | 9.Replacing string on a range of lines : You can specify a range of line numbers to the sed command for replacing a string. |
| 321 | |
| 322 | `$sed '1,3 s/unix/linux/' geekfile.txt` |
| 323 | |
| 324 | 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 |
| 325 | Examples: |
| 326 | 1.To Delete a particular line say n in this example |
| 327 | Syntax: |
| 328 | `$ sed 'nd' filename.txt` |
| 329 | |
| 330 | Example: |
| 331 | `$ sed '5d' filename.txt` |
| 332 | |
| 333 | 2. To Delete a last line |
| 334 | |
| 335 | Syntax: |
| 336 | |
| 337 | `$ sed '$d' filename.txt` |
| 338 | |
| 339 | 3. To Delete line from range x to y |
| 340 | |
| 341 | Syntax: |
| 342 | |
| 343 | `$ sed 'x,yd' filename.txt` |
| 344 | |
| 345 | Example: |
| 346 | |
| 347 | `$ sed '3,6d' filename.txt` |
| 348 | |
| 349 | 4. To Delete from nth to last line |
| 350 | |
| 351 | Syntax: |
| 352 | |
| 353 | `$ sed 'nth,$d' filename.txt` |
| 354 | |
| 355 | Example: |
| 356 | |
| 357 | `$ sed '12,$d' filename.txt` |
| 358 | |
| 359 | 5. To Delete pattern matching line |
| 360 | |
| 361 | Syntax: |
| 362 | `$ sed '/pattern/d' filename.txt` |
| 363 | |
| 364 | Example: |
| 365 | |
| 366 | `$ sed '/abc/d' filename.txt` |
| 367 | |
| 368 | |
| 369 | |