Changes between Version 10 and Version 11 of Csle2022/Agenda/linuxpackagemanagement


Ignore:
Timestamp:
Oct 19, 2022, 7:15:16 PM (2 years ago)
Author:
deepthi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Csle2022/Agenda/linuxpackagemanagement

    v10 v11  
    281281==== sed ====
    282282
     283SED 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
     2881.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
     2922. 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
     2963.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
     3004.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
     3045.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
     3086.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
     3127.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
     3168.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
     3209.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
     32410.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
     325Examples:
     326 1.To Delete a particular line say n in this example
     327Syntax:
     328`$ sed 'nd' filename.txt`
     329
     330Example:
     331`$ sed '5d' filename.txt`
     332
     3332. To Delete a last line
     334
     335Syntax:
     336
     337`$ sed '$d' filename.txt`
     338
     3393. To Delete line from range x to y
     340
     341Syntax:
     342
     343`$ sed 'x,yd' filename.txt`
     344
     345Example:
     346
     347`$ sed '3,6d' filename.txt`
     348
     3494. To Delete from nth to last line
     350
     351Syntax:
     352
     353`$ sed 'nth,$d' filename.txt`
     354
     355Example:
     356
     357`$ sed '12,$d' filename.txt`
     358
     3595. To Delete pattern matching line
     360
     361Syntax:
     362`$ sed '/pattern/d' filename.txt`
     363
     364Example:
     365
     366`$ sed '/abc/d' filename.txt`
     367
     368
     369
    283370==== awk ====