Advertisement
efxtv

CUT command in LINUX

Sep 20th, 2023 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | Cybersecurity | 0 0
  1. Q. What do you mean by the CUT command in LINUX?
  2. A. It helps remove sections from:
  3. - Cut Line and file (by byte position, character, and field)
  4. - Cut, slice, and extract the command and text
  5. - Saves it to Standard output.
  6.  
  7. Cut: Syntax
  8. -b select by bytes
  9. -c select by characters
  10. -f select by field
  11. -d select by delimiter/separator
  12. cut option [file_name]
  13.  
  14. -b CUT BY BYTES
  15. 1. How to cut BYTES from Right.
  16. a). Print first 4 BYTES, cut rest
  17. cut -b 1,2,3,4 file.txt
  18.  
  19. b). Print the first range of BYTES 1-5, cut rest
  20. cut -b 1-5 file.txt
  21.  
  22. c). Print first 1-3 and 5-7 BYTES, cut rest
  23. cut -b 1-3,5-7 file.txt
  24.  
  25. d). Print 3rd BYTES, cut rest
  26. cut -b 3 file.txt
  27.  
  28. e) Print up to 3rd BYTES using -
  29. cut -b -3 file.txt
  30.  
  31. f) Cut FIST BYTES, print rest
  32. cut -b 2- file.txt
  33.  
  34. -C CUT BY CHARACTER
  35.  
  36. 1. Print the 2nd and 5th character of each line, rest cut
  37. cut -c 2,5 file.txt
  38.  
  39. 2. Print 2nd to 5th character, cut rest
  40. cut -c 2-5 file.txt
  41.  
  42. -f CUT BY FIELDS RATHER THEN COLUMNS
  43. -d stands for delimiter (space, tab, commas (,), semicolon (;), quotes ( ", ' ), braces ({}), pipes (|), or slashes ( / \ )
  44.  
  45. Example of fields:
  46. cat file.txt
  47. Hello world
  48. hi words
  49. -f 1 = Field 1 Hello and hi
  50. -f 2 = Field 2 world and words
  51.  
  52. cat file.txt #(delimiter is space. there are 3 spaces in each line, represents first word" "2nd word" "3rd word)
  53.  
  54. Delimiter is space (" ") -d " "
  55. hello wolds boy
  56. world is hi
  57. news is no
  58. is not cool body
  59. bad thing pain
  60.  
  61. 1. Print all the 3rd words
  62. cut -d " " -f 3 file.txt
  63.  
  64. 2. Print all the 2nd words
  65. cut -d " " -f 2 file.txt
  66.  
  67. 3. Print 1st and 2nd-word
  68. cut -d " " -f 1,2 file.txt
  69.  
  70. 4. Print 1st to 3rd-word
  71. cut -d " " -f 1-3 file.txt
  72.  
  73. Delimiter is tab ($'\t') -d$'\t'
  74. hello wolds boy
  75. world is hi
  76. news is no
  77. is not a cool body
  78. bad thing pain
  79.  
  80. 1. Print all the 3rd words
  81. cut -d$'\t' -f 1,3 file.txt
  82.  
  83. 2. Print all the 2nd words
  84. cut -d$'\t' -f 2 file.txt
  85.  
  86. 3. Print 1st and 2nd word
  87. cut -d$'\t' -f 1,2 file.txt
  88.  
  89. 4. Print 1st to 3rd word
  90. cut -d$'\t' -f 1-3 file.txt
  91.  
  92. Delimiter is : (":") -d ":"
  93. hello:wolds:boy
  94. world:is:hi
  95. news:is:no
  96. isnot:cool:body
  97. bad:thing:pain
  98.  
  99. 1. Print all the 3rd words
  100. cut -d ':' -f 1,3 file.txt
  101.  
  102. 2. Print all the 2nd words
  103. cut -d ':' -f 2 file.txt
  104.  
  105. 3. Print 1st and 2nd-word
  106. cut -d ':' -f 1,2 file.txt
  107.  
  108. 4. Print 1st to 3rd-word
  109. cut -d ':' -f 1-3 file.txt
  110.  
  111. 5. Print 1st to 3rd-word and 3-4
  112. cut -d ':' -f 1-3,3-4 file.txt
  113.  
  114. 6. Print 1st to 3rd
  115. cut -d ':' -f -3 file.txt
  116.  
  117. 7. Print from 3rd to last
  118. cut -d ':' -f 3- file.txt
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement