Advertisement
efxtv

500 Most used sed Commands

Apr 22nd, 2025 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.26 KB | Cybersecurity | 0 0
  1. Top 500 Useful Sed commands
  2.  
  3. ##################################################################
  4. Join our telegram channel for more : https://t.me/LinuxClassesEFXTv
  5. ##################################################################
  6.  
  7. Certainly! Here's a list of `sed` commands you may not know
  8.  
  9. 1. Substitute a string in a file
  10. `$ sed 's/old_string/new_string/' filename`
  11.  
  12. 2. Substitute a string globally (across the entire line)
  13. `$ sed 's/old_string/new_string/g' filename`
  14.  
  15. 3. Substitute a string in a specific line number
  16. `$ sed '3s/old_string/new_string/' filename`
  17.  
  18. 4. Substitute a string in a range of line numbers
  19. `$ sed '2,5s/old_string/new_string/' filename`
  20.  
  21. 5. Delete a specific line
  22. `$ sed '3d' filename`
  23.  
  24. 6. Delete a range of lines
  25. `$ sed '2,5d' filename`
  26.  
  27. 7. Print only specific lines
  28. `$ sed -n '2,5p' filename`
  29.  
  30. 8. Print all lines except a range of lines
  31. `$ sed '2,5!p' filename`
  32.  
  33. 9. Delete empty lines
  34. `$ sed '/^$/d' filename`
  35.  
  36. 10. Remove leading whitespaces
  37. `$ sed 's/^[ \t]//' filename`
  38.  
  39. 11. Remove trailing whitespaces
  40. `$ sed 's/[ \t]$//' filename`
  41.  
  42. 12. Remove both leading and trailing whitespaces
  43. `$ sed 's/^[ \t]//;s/[ \t]$//' filename`
  44.  
  45. 13. Substitute string with case-insensitivity
  46. `$ sed 's/old_string/new_string/Ig' filename`
  47.  
  48. 14. Replace only the first occurrence of a string
  49. `$ sed '0,/old_string/s//new_string/' filename`
  50.  
  51. 15. Substitute on lines matching a pattern
  52. `$ sed '/pattern/s/old_string/new_string/' filename`
  53.  
  54. 16. Append a line after a specific line
  55. `$ sed '3a\This is a new line' filename`
  56.  
  57. 17. Prepend a line before a specific line
  58. `$ sed '3i\This is a new line' filename`
  59.  
  60. 18. Change a line entirely
  61. `$ sed '3c\This is the new line' filename`
  62.  
  63. 19. Remove lines matching a pattern
  64. `$ sed '/pattern/d' filename`
  65.  
  66. 20. Replace the nth occurrence of a string
  67. `$ sed 's/old_string/new_string/2' filename`
  68.  
  69. 21. Replace text and write output to a new file
  70. `$ sed 's/old_string/new_string/g' filename > newfile`
  71.  
  72. 22. Save the modified file in-place
  73. `$ sed -i 's/old_string/new_string/g' filename`
  74.  
  75. 23. Create a backup when modifying the file in-place
  76. `$ sed -i.bak 's/old_string/new_string/g' filename`
  77.  
  78. 24. Remove a line that starts with a specific pattern
  79. `$ sed '/^pattern/d' filename`
  80.  
  81. 25. Remove a line that ends with a specific pattern
  82. `$ sed '/pattern$/d' filename`
  83.  
  84. 26. Replace a word with another word in a file
  85. `$ sed 's/old_word/new_word/g' filename`
  86.  
  87. 27. Escape special characters in `sed`
  88. `$ sed 's/\//\\\//g' filename`
  89.  
  90. 28. Insert a line after every line containing a pattern
  91. `$ sed '/pattern/a\This is the new line' filename`
  92.  
  93. 29. Replace every nth word in a line
  94. `$ sed 's/\(\w\+\) \(\w\+\)/\1 replaced \2/' filename`
  95.  
  96. 30. Remove specific columns from a text
  97. `$ sed 's/\([^ ]\) \([^ ]\) /\1 /' filename`
  98.  
  99. 31. Remove all non-alphanumeric characters
  100. `$ sed 's/[^a-zA-Z0-9]//g' filename`
  101.  
  102. 32. Insert text at the beginning of every line
  103. `$ sed 's/^/Text at start /' filename`
  104.  
  105. 33. Insert text at the end of every line
  106. `$ sed 's/$/ Text at end/' filename`
  107.  
  108. 34. Replace only lines that contain a certain string
  109. `$ sed '/pattern/s/old_string/new_string/g' filename`
  110.  
  111. 35. Remove all digits from a file
  112. `$ sed 's/[0-9]//g' filename`
  113.  
  114. 36. Replace newline with a space
  115. `$ sed ':a;N;$!ba;s/\n/ /g' filename`
  116.  
  117. 37. Show line numbers with the output
  118. `$ sed = filename | sed 'N;s/\n/ /'`
  119.  
  120. 38. Print specific lines by line number
  121. `$ sed -n '5p' filename`
  122.  
  123. 39. Replace multiple spaces with a single space
  124. `$ sed 's/ \+/ /g' filename`
  125.  
  126. 40. Show the line before and after the match
  127. `$ sed -n '/pattern/{=;p;}' filename`
  128.  
  129. 41. Extract lines that match a pattern
  130. `$ sed -n '/pattern/p' filename`
  131.  
  132. 42. Add line numbers to each line
  133. `$ sed = filename | sed 'N;s/\n/\t/'`
  134.  
  135. 43. Remove all uppercase letters
  136. `$ sed 's/[A-Z]//g' filename`
  137.  
  138. 44. Replace the first word in each line
  139. `$ sed 's/^\w\+/new_word/' filename`
  140.  
  141. 45. Replace the last word in each line
  142. `$ sed 's/\w\+$/new_word/' filename`
  143.  
  144. 46. Delete lines between two patterns
  145. `$ sed '/start_pattern/,/end_pattern/d' filename`
  146.  
  147. 47. Replace a string in the file without case sensitivity
  148. `$ sed 's/old_string/new_string/I' filename`
  149.  
  150. 48. Substitute with special characters
  151. `$ sed 's/[0-9]/#/g' filename`
  152.  
  153. 49. Print the line before the matching pattern
  154. `$ sed -n '/pattern/{x;p;x;}' filename`
  155.  
  156. 50. Replace all punctuation with spaces
  157. `$ sed 's/[[:punct:]]/ /g' filename`
  158.  
  159. 51. Add a line after the last line
  160. `$ sed '$a\This is the last line' filename`
  161.  
  162. 52. Remove the first line from the file
  163. `$ sed '1d' filename`
  164.  
  165. 53. Remove the last line from the file
  166. `$ sed '$d' filename`
  167.  
  168. 54. Replace first character in each line
  169. `$ sed 's/^./new_char/' filename`
  170.  
  171. 55. Replace last character in each line
  172. `$ sed 's/.$/new_char/' filename`
  173.  
  174. 56. Add text before a matching line
  175. `$ sed '/pattern/i\Text before pattern' filename`
  176.  
  177. 57. Replace the nth occurrence of a pattern on all lines
  178. `$ sed 's/pattern/replacement/3' filename`
  179.  
  180. 58. Replace pattern with a newline
  181. `$ sed 's/pattern/\n/g' filename`
  182.  
  183. 59. Remove all comments (lines starting with `#`)
  184. `$ sed '/^#/d' filename`
  185.  
  186. 60. Remove lines with a specific word
  187. `$ sed '/word/d' filename`
  188.  
  189. 61. Display lines containing a specific pattern
  190. `$ sed -n '/pattern/p' filename`
  191.  
  192. 62. Remove blank lines
  193. `$ sed '/^\s$/d' filename`
  194.  
  195. 63. Replace string only on lines that don't match another pattern
  196. `$ sed '/pattern/!s/old_string/new_string/' filename`
  197.  
  198. 64. Substitute a string only on the first line
  199. `$ sed '1s/old_string/new_string/' filename`
  200.  
  201. 65. Add a new line after every match
  202. `$ sed '/pattern/a\New line after match' filename`
  203.  
  204. 66. Add a new line before every match
  205. `$ sed '/pattern/i\New line before match' filename`
  206.  
  207. 67. Insert a line at a specific line number
  208. `$ sed '3i\Insert line here' filename`
  209.  
  210. 68. Delete lines matching a pattern globally
  211. `$ sed '/pattern/d' filename`
  212.  
  213. 69. Replace specific word with a variable
  214. `$ sed "s/old_word/$variable/" filename`
  215.  
  216. 70. Replace string globally across multiple files
  217. `$ sed -i 's/old_string/new_string/g' .txt`
  218.  
  219. 71. Print lines that don’t match a pattern
  220. `$ sed -n '/pattern/!p' filename`
  221.  
  222. 72. Delete lines matching multiple patterns
  223. `$ sed '/pattern1\|pattern2/d' filename`
  224.  
  225. 73. Replace a word with multiple spaces
  226. `$ sed 's/word/ /g' filename`
  227.  
  228. 74. Remove all spaces from a file
  229. `$ sed 's/ //g' filename`
  230.  
  231. 75. Replace a string in the middle of a line
  232. `$ sed 's/middle_string/new_string/' filename`
  233.  
  234. 76. Add a prefix to every line
  235. `$ sed 's/^/Prefix /' filename`
  236.  
  237. 77. Add a suffix to every line
  238. `$ sed 's/$/ Suffix/' filename`
  239.  
  240. 78. Remove everything after the first space on each line
  241. `$ sed 's/ .$//' filename`
  242.  
  243. 79. Remove everything before the first space on each line
  244. `$ sed 's/^[^ ] //' filename`
  245.  
  246. 80. Remove a word in a sentence
  247. `$ sed 's/\bword\b//g' filename`
  248.  
  249. 81. Convert lowercase to uppercase
  250. `$ sed 's/./\U&/' filename`
  251.  
  252. 82. Convert uppercase to lowercase
  253. `$ sed 's/./\L&/' filename`
  254.  
  255. 83. Delete lines from a specific line number to the end of the file
  256. `$ sed '3,$d' filename`
  257.  
  258. 84. Delete every nth line
  259. `$ sed '0~3d' filename`
  260.  
  261. 85. Replace a string across multiple files in a directory
  262. `$ sed -i 's/old_string/new_string/g' /path/to/files/.txt`
  263.  
  264. 86. Match words that start with a specific character
  265. `$ sed 's/\bA\w/new_string/g' filename`
  266.  
  267. 87. Replace a word globally and display result
  268. `$ sed -n 's/old_word/new_word/gp' filename`
  269.  
  270. 88. Print the first line only
  271. `$ sed -n '1p' filename`
  272.  
  273. 89. Print the last line only
  274. `$ sed -n '$p' filename`
  275.  
  276. 90. Print the nth line only
  277. `$ sed -n 'n p' filename`
  278.  
  279. 91. Replace the string using a regex pattern
  280. `$ sed 's/\b[a-zA-Z]\{5\}\b/replacement/g' filename`
  281.  
  282. 92. Replace a string but exclude a specific pattern
  283. `$ sed '/exclude_pattern/!s/old_string/new_string/' filename`
  284.  
  285. 93. Count occurrences of a pattern
  286. `$ sed -n '/pattern/{s//&/g;p}' filename | wc -l`
  287.  
  288. 94. Add a suffix before the last character
  289. `$ sed 's/\(.\)/\1Suffix/' filename`
  290.  
  291. 95. Insert a line before the last line
  292. `$ sed '$i\Line before last' filename`
  293.  
  294. 96. Replace the first word of every line with a specific word
  295. `$ sed 's/^\w\+/new_word/' filename`
  296.  
  297. 97. Add a line after the first match of a pattern
  298. `$ sed '/pattern/a\New line after first match' filename`
  299.  
  300. 98. Remove specific words from each line
  301. `$ sed 's/\bword1\b//g; s/\bword2\b//g' filename`
  302.  
  303. 99. Remove specific pattern and its following content
  304. `$ sed 's/pattern.$//' filename`
  305.  
  306. 100. Insert a line after a specific pattern match
  307. `$ sed '/pattern/a\Insert line after match' filename`
  308.  
  309. 101. Replace only the first line in the file
  310. `$ sed '1s/old_string/new_string/' filename`
  311.  
  312. 102. Remove all vowels from a file
  313. `$ sed 's/[aeiouAEIOU]//g' filename`
  314.  
  315. 103. Replace all digits with `#`
  316. `$ sed 's/[0-9]/#/g' filename`
  317.  
  318. 104. Add a new line after every nth line
  319. `$ sed 'n a\New line after nth' filename`
  320.  
  321. 105. Replace a string and print line numbers
  322. `$ sed -n 's/old_string/new_string/p' filename`
  323.  
  324. 106. Remove the first and last character from each line
  325. `$ sed 's/^.\(.\).$/\1/' filename`
  326.  
  327. 107. Remove blank lines and lines with only spaces
  328. `$ sed '/^[[:space:]]$/d' filename`
  329.  
  330. 108. Replace only in lines that do not contain a pattern
  331. `$ sed '/pattern/!s/old_string/new_string/' filename`
  332.  
  333. 109. Replace the last occurrence of a string
  334. `$ sed 's/\(.\)old_string/\1new_string/' filename`
  335.  
  336. 110. Print only lines matching a pattern
  337. `$ sed -n '/pattern/p' filename`
  338.  
  339. 111. Print lines starting with a specific character
  340. `$ sed -n '/^A/p' filename`
  341.  
  342. 112. Remove the first occurrence of a string
  343. `$ sed 's/old_string//' filename`
  344.  
  345. 113. Replace a pattern using a variable
  346. `$ sed "s/$old_string/$new_string/" filename`
  347.  
  348. 114. Print line numbers along with content
  349. `$ sed '=' filename | sed 'N;s/\n/\t/'`
  350.  
  351. 115. Replace multiple spaces with a single space
  352. `$ sed 's/ / /g' filename`
  353.  
  354. 116. Convert all spaces to underscores
  355. `$ sed 's/ /_/g' filename`
  356.  
  357. 117. Escape forward slashes
  358. `$ sed 's/\//\\\//g' filename`
  359.  
  360. 118. Remove non-alphanumeric characters
  361. `$ sed 's/[^a-zA-Z0-9]//g' filename`
  362.  
  363. 119. Replace newlines with a space
  364. `$ sed ':a;N;$!ba;s/\n/ /g' filename`
  365.  
  366. 120. Insert content before a specific string match
  367. `$ sed '/pattern/i\Insert before pattern' filename`
  368.  
  369. 121. Replace the nth character in a line
  370. `$ sed 's/\(.\{n\}\).\{1\}/\1replacement/' filename`
  371.  
  372. 122. Print lines that end with a specific string
  373. `$ sed -n '/pattern$/p' filename`
  374.  
  375. 123. Print the first n lines
  376. `$ sed -n '1,5p' filename`
  377.  
  378. 124. Remove every nth line
  379. `$ sed 'n!d' filename`
  380.  
  381. 125. Add text to the beginning of the file
  382. `$ sed -i '1s/^/Text at the start of the file\n/' filename`
  383.  
  384. 126. Replace a string and ignore case sensitivity
  385. `$ sed 's/old_string/new_string/I' filename`
  386.  
  387. 127. Remove lines containing a pattern from a specific file
  388. `$ sed '/pattern/d' filename`
  389.  
  390. 128. Substitute using extended regular expressions
  391. `$ sed -r 's/(pattern)/replacement/' filename`
  392.  
  393. 129. Replace text in a specific column
  394. `$ sed 's/^\([^\t]\)\t\([^\t]\)$/\1\tnew_text/' filename`
  395.  
  396. 130. Replace all spaces with hyphens
  397. `$ sed 's/ /-/g' filename`
  398.  
  399. 131. Replace all instances of a string and show result
  400. `$ sed 's/old_string/new_string/gp' filename`
  401.  
  402. 132. Change the case of every word
  403. `$ sed 's/\(.\)/\U&/' filename`
  404.  
  405. 133. Replace all punctuation marks
  406. `$ sed 's/[[:punct:]]/ /g' filename`
  407.  
  408. 134. Replace word at the beginning of each line
  409. `$ sed 's/^word/new_word/' filename`
  410.  
  411. 135. Reverse every line in a file
  412. `$ sed 's/\(.\)/\1/' filename | rev`
  413.  
  414. 136. Remove empty lines but not lines with spaces
  415. `$ sed '/^[[:space:]]$/d' filename`
  416.  
  417. 137. Print lines with a specific number of words
  418. `$ sed -n '/\w\{5\}/p' filename`
  419.  
  420. 138. Change words between specific tags
  421. `$ sed 's/<tag>\(.\)<\/tag>/<tag>new_text<\/tag>/' filename`
  422.  
  423. 139. Replace first letter of each word with a capital
  424. `$ sed 's/\b\(.\)/\U\1/g' filename`
  425.  
  426. 140. Replace multiple spaces with a single space using a regex
  427. `$ sed 's/\s\+/ /g' filename`
  428.  
  429. 141. Substitute multiple occurrences on multiple lines
  430. `$ sed ':a;N;$!ba;s/old_string/new_string/g' filename`
  431.  
  432. 142. Remove text between two patterns
  433. `$ sed 's/pattern1.pattern2//g' filename`
  434.  
  435. 143. Add a blank line between lines matching a pattern
  436. `$ sed '/pattern/a\ ' filename`
  437.  
  438. 144. Display content of specific line and its preceding line
  439. `$ sed -n '2p; 1p' filename`
  440.  
  441. 145. Search and replace using `-e` (expression)
  442. `$ sed -e 's/old/new/' filename`
  443.  
  444. 146. Replace multiple patterns on one line
  445. `$ sed 's/old1/new1; s/old2/new2/' filename`
  446.  
  447. 147. Reverse the contents of a file
  448. `$ sed 's/./&/' filename | rev`
  449.  
  450. 148. Replace the nth instance of a word in a string
  451. `$ sed 's/word/new_word/n' filename`
  452.  
  453. 149. Replace string but print before and after
  454. `$ sed 's/old_string/new_string/p' filename`
  455.  
  456. 150. Replace specific pattern with the content from a file
  457. `$ sed 's/pattern/$(cat replacement_file)/' filename`
  458.  
  459. 151. Replace the nth occurrence of a string only
  460. `$ sed 's/old_string/new_string/3' filename`
  461.  
  462. 152. Remove leading spaces from each line
  463. `$ sed 's/^[ \t]//' filename`
  464.  
  465. 153. Remove trailing spaces from each line
  466. `$ sed 's/[ \t]$//' filename`
  467.  
  468. 154. Remove all spaces
  469. `$ sed 's/ //g' filename`
  470.  
  471. 155. Replace string in a case-insensitive manner
  472. `$ sed 's/old_string/new_string/I' filename`
  473.  
  474. 156. Delete lines matching a regular expression
  475. `$ sed '/pattern/d' filename`
  476.  
  477. 157. Print the lines matching a specific pattern
  478. `$ sed -n '/pattern/p' filename`
  479.  
  480. 158. Replace all occurrences of a pattern across multiple files
  481. `$ sed -i 's/old_string/new_string/g' .txt`
  482.  
  483. 159. Add a prefix to all lines
  484. `$ sed 's/^/Prefix: /' filename`
  485.  
  486. 160. Remove empty lines
  487. `$ sed '/^$/d' filename`
  488.  
  489. 161. Remove lines containing a specific word
  490. `$ sed '/word/d' filename`
  491.  
  492. 162. Add a new line before every line that matches a pattern
  493. `$ sed '/pattern/i\New line before match' filename`
  494.  
  495. 163. Substitute the last word of every line
  496. `$ sed 's/\w$/new_word/' filename`
  497.  
  498. 164. Remove the first word from each line
  499. `$ sed 's/^\w\+ //' filename`
  500.  
  501. 165. Remove all non-alphanumeric characters
  502. `$ sed 's/[^a-zA-Z0-9]//g' filename`
  503.  
  504. 166. Remove all digits from a file
  505. `$ sed 's/[0-9]//g' filename`
  506.  
  507. 167. Add text to the end of each line
  508. `$ sed 's/$/ appended text/' filename`
  509.  
  510. 168. Convert all letters to uppercase
  511. `$ sed 's/./\U&/' filename`
  512.  
  513. 169. Convert all letters to lowercase
  514. `$ sed 's/./\L&/' filename`
  515.  
  516. 170. Add line numbers to the beginning of each line
  517. `$ sed = filename | sed 'N;s/\n/\t/'`
  518.  
  519. 171. Print only the nth line
  520. `$ sed -n 'n p' filename`
  521.  
  522. 172. Remove the first and last character of each line
  523. `$ sed 's/^.\(.\).$/\1/' filename`
  524.  
  525. 173. Remove duplicate lines
  526. `$ sed '$!N; /^\(.\)\n\1$/!P; D' filename`
  527.  
  528. 174. Replace spaces between words with underscores
  529. `$ sed 's/ /_/g' filename`
  530.  
  531. 175. Replace multiple spaces with a single space
  532. `$ sed 's/ / /g' filename`
  533.  
  534. 176. Replace text in lines that do not match a pattern
  535. `$ sed '/pattern/!s/old_string/new_string/' filename`
  536.  
  537. 177. Replace text only in the first line
  538. `$ sed '1s/old_string/new_string/' filename`
  539.  
  540. 178. Show the line before a match
  541. `$ sed -n '/pattern/{x;p;x;}' filename`
  542.  
  543. 179. Remove all spaces at the beginning of each line
  544. `$ sed 's/^[ \t]//' filename`
  545.  
  546. 180. Insert text after each line containing a pattern
  547. `$ sed '/pattern/a\Inserted text after match' filename`
  548.  
  549. 181. Insert text before each line containing a pattern
  550. `$ sed '/pattern/i\Inserted text before match' filename`
  551.  
  552. 182. Match a line that starts with a specific word
  553. `$ sed -n '/^word/p' filename`
  554.  
  555. 183. Match a line that ends with a specific word
  556. `$ sed -n '/word$/p' filename`
  557.  
  558. 184. Remove a specific word from the file
  559. `$ sed 's/\bword\b//g' filename`
  560.  
  561. 185. Remove the last line in a file
  562. `$ sed '$d' filename`
  563.  
  564. 186. Remove the first line in a file
  565. `$ sed '1d' filename`
  566.  
  567. 187. Print lines containing a word but not matching a pattern
  568. `$ sed -n '/word/!p' filename`
  569.  
  570. 188. Replace a string and display result on the terminal
  571. `$ sed 's/old_string/new_string/gp' filename`
  572.  
  573. 189. Replace text in a specific line number and print it
  574. `$ sed '3s/old_string/new_string/p' filename`
  575.  
  576. 190. Print the first 10 lines of the file
  577. `$ sed -n '1,10p' filename`
  578.  
  579. 191. Replace a word with a newline character
  580. `$ sed 's/old_word/\n/g' filename`
  581.  
  582. 192. Show lines with a specific pattern but exclude others
  583. `$ sed -n '/pattern/{p;}' filename`
  584.  
  585. 193. Count number of occurrences of a word in a file
  586. `$ sed -n 's/word/&/gp' filename | wc -l`
  587.  
  588. 194. Change a string with a random value
  589. `$ sed 's/old_string/$(shuf -n 1 value_list)/' filename`
  590.  
  591. 195. Print only lines that match the regular expression
  592. `$ sed -n '/pattern/p' filename`
  593.  
  594. 196. Replace the first character of the line
  595. `$ sed 's/^./new_char/' filename`
  596.  
  597. 197. Replace the last character of the line
  598. `$ sed 's/.$/new_char/' filename`
  599.  
  600. 198. Replace the nth occurrence of a string
  601. `$ sed 's/old_string/new_string/2' filename`
  602.  
  603. 199. Print the last line of the file
  604. `$ sed -n '$p' filename`
  605.  
  606. 200. Print lines after a match
  607. `$ sed -n '/pattern/,$p' filename`
  608.  
  609. 201. Replace all occurrences of a string in the file
  610. `$ sed 's/old_string/new_string/g' filename`
  611.  
  612. 202. Replace multiple words on each line
  613. `$ sed 's/old1/new1; s/old2/new2/g' filename`
  614.  
  615. 203. Change case of every word in a line
  616. `$ sed 's/\(.\)/\U&/' filename`
  617.  
  618. 204. Remove a specific line from the file
  619. `$ sed '3d' filename`
  620.  
  621. 205. Replace each occurrence of a word with different values
  622. `$ sed 's/word1/replacement1/; s/word2/replacement2/' filename`
  623.  
  624. 206. Replace a word but keep spaces intact
  625. `$ sed 's/\bword\b/&/' filename`
  626.  
  627. 207. Delete lines containing the word 'pattern'
  628. `$ sed '/pattern/d' filename`
  629.  
  630. 208. Print lines matching a pattern with line numbers
  631. `$ sed -n '/pattern/{=; p;}' filename`
  632.  
  633. 209. Replace the word only on the second line
  634. `$ sed '2s/old_word/new_word/' filename`
  635.  
  636. 210. Remove every other line from the file
  637. `$ sed 'n!d' filename`
  638.  
  639. 211. Print the file in reverse order
  640. `$ sed '1!G;h;$!d' filename`
  641.  
  642. 212. Match a line that does not start with a specific word
  643. `$ sed -n '/^[^word]/p' filename`
  644.  
  645. 213. Add a suffix to every line
  646. `$ sed 's/$/Suffix/' filename`
  647.  
  648. 214. Substitute spaces with commas
  649. `$ sed 's/ /,/g' filename`
  650.  
  651. 215. Add a prefix to every line that does not match a pattern
  652. `$ sed '/pattern/!s/^/Prefix: /' filename`
  653.  
  654. 216. Remove all digits from the file
  655. `$ sed 's/[0-9]//g' filename`
  656.  
  657. 217. Insert content after specific line number
  658. `$ sed '3a\New content' filename`
  659.  
  660. 218. Replace a specific string and ignore case
  661. `$ sed 's/old_string/new_string/I' filename`
  662.  
  663. 219. Print the nth line and the next one
  664. `$ sed -n 'n,+1p' filename`
  665.  
  666. 220. Replace a pattern with the content from another file
  667. `$ sed 's/pattern/$(<file)/' filename`
  668.  
  669. 221. Remove all lines containing only whitespace
  670. `$ sed '/^[[:space:]]$/d' filename`
  671.  
  672. 222. Add a line before every occurrence of a pattern
  673. `$ sed '/pattern/i\New line before match' filename`
  674.  
  675. 223. Substitute a string but only on the last line
  676. `$ sed '$s/old_string/new_string/' filename`
  677.  
  678. 224. Remove text after the first occurrence of a string
  679. `$ sed 's/old_string.$//' filename`
  680.  
  681. 225. Match lines with a specific number of characters
  682. `$ sed -n '/^.\{5\}$/p' filename`
  683.  
  684. 226. Remove all special characters
  685. `$ sed 's/[^a-zA-Z0-9]//g' filename`
  686.  
  687. 227. Replace all occurrences of a string in the first 10 lines
  688. `$ sed '1,10s/old_string/new_string/g' filename`
  689.  
  690. 228. Delete lines with a specific number of characters
  691. `$ sed '/^.\{10\}$/d' filename`
  692.  
  693. 229. Remove comments from shell scripts
  694. `$ sed '/^#/d' filename`
  695.  
  696. 230. Insert content at the beginning of the file
  697. `$ sed -i '1s/^/New content\n/' filename`
  698.  
  699. 231. Change string in specific columns
  700. `$ sed 's/\(\w\+\) \(\w\+\)/\1 \2 replacement/' filename`
  701.  
  702. 232. Substitute using backreference
  703. `$ sed 's/\(pattern\)/\1 replacement/' filename`
  704.  
  705. 233. Remove trailing spaces from each line
  706. `$
  707.  
  708. sed 's/[ \t]$//' filename`
  709.  
  710. 234. Replace all occurrences of a string in the first 5 lines
  711. `$ sed '1,5s/old_string/new_string/g' filename`
  712.  
  713. 235. Show the lines from 10 to 20
  714. `$ sed -n '10,20p' filename`
  715.  
  716. 236. Replace text globally excluding a pattern
  717. `$ sed '/pattern/!s/old_string/new_string/' filename`
  718.  
  719. 237. Remove a range of lines from a file
  720. `$ sed '3,7d' filename`
  721.  
  722. 238. Count the number of occurrences of a pattern
  723. `$ sed -n 's/pattern/&/gp' filename | wc -l`
  724.  
  725. 239. Insert a line at the end of the file
  726. `$ sed -e '\$a\Last line' filename`
  727.  
  728. 240. Delete all lines with a specific length
  729. `$ sed '/^.\{5\}$/d' filename`
  730.  
  731. 241. Replace the first occurrence of a string on the last line only
  732. `$ sed '$s/old_string/new_string/' filename`
  733.  
  734. 242. Remove all non-alphanumeric characters except space
  735. `$ sed 's/[^a-zA-Z0-9 ]//g' filename`
  736.  
  737. 243. Replace string in a specific line by line number
  738. `$ sed '5s/old_string/new_string/' filename`
  739.  
  740. 244. Substitute the first word on each line
  741. `$ sed 's/^\w\+/new_word/' filename`
  742.  
  743. 245. Replace a string in lines that do not match a pattern
  744. `$ sed '/pattern/!s/old_string/new_string/' filename`
  745.  
  746. 246. Find and print line numbers that match a pattern
  747. `$ sed -n '/pattern/=' filename`
  748.  
  749. 247. Remove empty lines and lines with only spaces
  750. `$ sed '/^[[:space:]]$/d' filename`
  751.  
  752. 248. Append a line after every line that matches a pattern
  753. `$ sed '/pattern/a\New appended line' filename`
  754.  
  755. 249. Replace a string in lines before a pattern
  756. `$ sed '/pattern/!s/old_string/new_string/' filename`
  757.  
  758. 250. Replace first occurrence of a string on specific line range
  759. `$ sed '1,5s/old_string/new_string/' filename`
  760.  
  761. 251. Remove multiple spaces between words
  762. `$ sed 's/ / /g' filename`
  763.  
  764. 252. Add a suffix to every line that matches a pattern
  765. `$ sed '/pattern/s/$/ suffix/' filename`
  766.  
  767. 253. Print lines after a match and include the matched line
  768. `$ sed -n '/pattern/,$p' filename`
  769.  
  770. 254. Replace all vowels with another character
  771. `$ sed 's/[aeiouAEIOU]/x/g' filename`
  772.  
  773. 255. Print lines starting with a number
  774. `$ sed -n '/^[0-9]/p' filename`
  775.  
  776. 256. Replace a string only in the first 10 lines
  777. `$ sed '1,10s/old_string/new_string/' filename`
  778.  
  779. 257. Print lines that do not contain a pattern
  780. `$ sed -n '/pattern/!p' filename`
  781.  
  782. 258. Replace space with newline after each word
  783. `$ sed 's/ /\'$'\n/g' filename`
  784.  
  785. 259. Print lines before a specific pattern
  786. `$ sed -n '1,/pattern/p' filename`
  787.  
  788. 260. Replace only the first occurrence of a pattern in the file
  789. `$ sed '0,/old_string/s//new_string/' filename`
  790.  
  791. 261. Print the first line of a file
  792. `$ sed -n '1p' filename`
  793.  
  794. 262. Remove the second column from a file
  795. `$ sed 's/\([^ ] [^ ]\) \(.\)/\1/' filename`
  796.  
  797. 263. Insert a line before every line that contains a string
  798. `$ sed '/pattern/i\New line before match' filename`
  799.  
  800. 264. Replace the nth word in each line
  801. `$ sed 's/\(\(\S\s\)\{n-1\}\)\S/\1new_word/' filename`
  802.  
  803. 265. Delete lines that start with a specific word
  804. `$ sed '/^word/d' filename`
  805.  
  806. 266. Remove all lines except the ones containing a pattern
  807. `$ sed '/pattern/!d' filename`
  808.  
  809. 267. Replace only the last occurrence of a string in the file
  810. `$ sed 's/old_string/new_string/g' filename | tac | sed 's/old_string/new_string/' | tac`
  811.  
  812. 268. Insert a new line at the beginning of a file
  813. `$ sed -i '1i\New beginning line' filename`
  814.  
  815. 269. Replace specific word from each line by skipping lines that match a pattern
  816. `$ sed '/pattern/!s/old_string/new_string/' filename`
  817.  
  818. 270. Add a prefix to every line
  819. `$ sed 's/^/Prefix /' filename`
  820.  
  821. 271. Add a suffix to lines containing a specific pattern
  822. `$ sed '/pattern/s/$/ suffix/' filename`
  823.  
  824. 272. Print the last n lines of the file
  825. `$ sed -n 'n,$p' filename`
  826.  
  827. 273. Find all occurrences of a pattern and print lines containing them
  828. `$ sed -n '/pattern/p' filename`
  829.  
  830. 274. Insert a new line after the first occurrence of a pattern
  831. `$ sed '/pattern/a\New line after first match' filename`
  832.  
  833. 275. Replace the nth occurrence in a line
  834. `$ sed 's/old_string/new_string/n' filename`
  835.  
  836. 276. Reverse the order of lines in a file
  837. `$ sed '1!G;h;$!d' filename`
  838.  
  839. 277. Replace the nth occurrence of a string across multiple lines
  840. `$ sed '0,/old_string/s//new_string/' filename`
  841.  
  842. 278. Replace a string across lines matching a pattern
  843. `$ sed '/pattern/ {s/old_string/new_string/}' filename`
  844.  
  845. 279. Remove characters until the first space
  846. `$ sed 's/^[^ ] //' filename`
  847.  
  848. 280. Replace the string on lines that do not match a pattern
  849. `$ sed '/pattern/!s/old_string/new_string/' filename`
  850.  
  851. 281. Print the first 10 lines of a file
  852. `$ sed -n '1,10p' filename`
  853.  
  854. 282. Remove all lines except the ones starting with a pattern
  855. `$ sed '/^pattern/!d' filename`
  856.  
  857. 283. Replace only the first word in each line
  858. `$ sed 's/^\w\+/new_word/' filename`
  859.  
  860. 284. Replace a string but preserve the case of the original
  861. `$ sed 's/old_string/\L&/g' filename`
  862.  
  863. 285. Remove everything after the first comma in each line
  864. `$ sed 's/,.//' filename`
  865.  
  866. 286. Remove characters after the nth character in a line
  867. `$ sed 's/\(.\{n\}\)./\1/' filename`
  868.  
  869. 287. Remove spaces and tabs before and after the content
  870. `$ sed 's/^[[:space:]]//;s/[[:space:]]$//' filename`
  871.  
  872. 288. Delete lines containing a pattern and print the result
  873. `$ sed '/pattern/d' filename`
  874.  
  875. 289. Append a string at the end of the nth line
  876. `$ sed 'n s/$/ appended_text/' filename`
  877.  
  878. 290. Replace the nth occurrence of a pattern in the file
  879. `$ sed 'n s/old_string/new_string/' filename`
  880.  
  881. 291. Print a specific line range from the file
  882. `$ sed -n '10,20p' filename`
  883.  
  884. 292. Remove everything after the first match of a string in each line
  885. `$ sed 's/\(.pattern\)./\1/' filename`
  886.  
  887. 293. Remove all but the nth occurrence of a word in the line
  888. `$ sed 's/\(.old_string\)\(old_string\)/\1new_string/' filename`
  889.  
  890. 294. Replace the first word with a string
  891. `$ sed 's/^\w\+/new_word/' filename`
  892.  
  893. 295. Change the last character of each line
  894. `$ sed 's/.$/new_char/' filename`
  895.  
  896. 296. Remove lines containing only digits
  897. `$ sed '/^[0-9]$/d' filename`
  898.  
  899. 297. Replace words of length 4 with a string
  900. `$ sed 's/\b\w\{4\}\b/new_string/g' filename`
  901.  
  902. 298. Replace all occurrences in the first 3 lines
  903. `$ sed '1,3s/old_string/new_string/g' filename`
  904.  
  905. 299. Replace text only on lines that do not contain a word
  906. `$ sed '/\bword\b/!s/old_string/new_string/' filename`
  907.  
  908. 300. Insert a line after a specific pattern in the file
  909. `$ sed '/pattern/a\New line after match' filename`
  910.  
  911. 301. Remove trailing spaces and tabs in the entire file
  912. `$ sed 's/[ \t]$//' filename`
  913.  
  914. 302. Print the lines before a specific pattern
  915. `$ sed -n '1,/pattern/p' filename`
  916.  
  917. 303. Remove the last character in each line
  918. `$ sed 's/.$//' filename`
  919.  
  920. 304. Insert a new line at the end of a file
  921. `$ sed -e '$a\New last line' filename`
  922.  
  923. 305. Replace nth occurrence of a string and print it
  924. `$ sed 'n s/old_string/new_string/' filename`
  925.  
  926. 306. Remove lines containing a specific pattern
  927. `$ sed '/pattern/d' filename`
  928.  
  929. 307. Change the last occurrence of a word in a file
  930. `$ sed 's/old_string/new_string/2' filename`
  931.  
  932. 308. Print lines that match a specific string pattern
  933. `$ sed -n '/pattern/p' filename`
  934.  
  935. 309. Print lines matching a specific word and add numbering
  936. `$ sed -n '/pattern/=' filename`
  937.  
  938. 310. Add a line at the start of the file
  939. `$ sed '1i\New first line' filename`
  940.  
  941. 311. Replace the first occurrence of a pattern on the last line
  942. `$ sed '$s/old_string/new_string/' filename`
  943.  
  944. 312. Replace a string and only print the result
  945. `$ sed 's/old_string/new_string/p' filename`
  946.  
  947. 313. Replace text globally but ignore case
  948. `$ sed 's/old_string/new_string/I' filename`
  949.  
  950. 314. Replace each word in a line with a specific string
  951. `$ sed 's/\w\+/new_string/g' filename`
  952.  
  953. 315. Replace every word that is longer than 3 characters
  954. `$ sed 's/\b\w\{4,\}\b/new_word/g' filename`
  955.  
  956. 316. Insert content after a specific line number
  957. `$ sed 'n a\New content' filename`
  958.  
  959. 317. Print the lines before and after a match
  960. `$ sed -n '/pattern/{x; p; x;}' filename`
  961.  
  962. 318. Replace the word with a string only in the first 5 lines
  963. `$ sed '1,5s/old_string/new_string/' filename`
  964.  
  965. 319. Remove the first occurrence of a string in the entire file
  966. `$ sed 's/old_string//' filename`
  967.  
  968. 320. Change a string in the first 5 lines but only print it
  969. `$ sed -n '1,5s/old_string/new_string/p' filename`
  970. 321. Replace only the first instance of a string
  971. `$ sed '0,/old_string/s//new_string/' filename`
  972.  
  973. 322. Print all lines except the last one
  974. `$ sed '$d' filename`
  975.  
  976. 323. Replace all occurrences of a string in the second half of the file
  977. `$ sed 'n,$s/old_string/new_string/g' filename`
  978.  
  979. 324. Remove all characters after the first comma
  980. `$ sed 's/,.//' filename`
  981.  
  982. 325. Replace all occurrences of a string from the first line to the 10th line
  983. `$ sed '1,10s/old_string/new_string/g' filename`
  984.  
  985. 326. Remove trailing spaces and tabs from all lines
  986. `$ sed 's/[[:space:]]$//' filename`
  987.  
  988. 327. Remove blank lines
  989. `$ sed '/^$/d' filename`
  990.  
  991. 328. Change the first occurrence of a string only on lines 10 to 20
  992. `$ sed '10,20s/old_string/new_string/' filename`
  993.  
  994. 329. Replace all tabs with spaces
  995. `$ sed 's/\t/ /g' filename`
  996.  
  997. 330. Replace all lowercase vowels with a number
  998. `$ sed 's/[aeiou]/1/g' filename`
  999.  
  1000. 331. Print all lines except for the ones containing a string
  1001. `$ sed '/pattern/!p' filename`
  1002.  
  1003. 332. Replace the first occurrence of a string on the last line of the file
  1004. `$ sed '$s/old_string/new_string/' filename`
  1005.  
  1006. 333. Find all the lines that do not contain a pattern
  1007. `$ sed '/pattern/!p' filename`
  1008.  
  1009. 334. Replace text globally in a file, but not on the last line
  1010. `$ sed '$!s/old_string/new_string/g' filename`
  1011.  
  1012. 335. Print the first line of the file
  1013. `$ sed -n '1p' filename`
  1014.  
  1015. 336. Add a line at the beginning of a file
  1016. `$ sed -i '1i\New line at the start' filename`
  1017.  
  1018. 337. Remove every second line from a file
  1019. `$ sed 'n!d' filename`
  1020.  
  1021. 338. Insert a line before every line containing a pattern
  1022. `$ sed '/pattern/i\New line before match' filename`
  1023.  
  1024. 339. Delete all lines that match a pattern
  1025. `$ sed '/pattern/d' filename`
  1026.  
  1027. 340. Replace the last occurrence of a string in the file
  1028. `$ sed 's/old_string/new_string/g' filename | tac | sed 's/old_string/new_string/' | tac`
  1029.  
  1030. 341. Print lines 5 through 10
  1031. `$ sed -n '5,10p' filename`
  1032.  
  1033. 342. Remove all lines except for the ones that match a pattern
  1034. `$ sed '/pattern/!d' filename`
  1035.  
  1036. 343. Replace the first word of every line with another word
  1037. `$ sed 's/^\w\+/new_word/' filename`
  1038.  
  1039. 344. Insert a string at the end of each line
  1040. `$ sed 's/$/New text/' filename`
  1041.  
  1042. 345. Replace every occurrence of a string globally
  1043. `$ sed 's/old_string/new_string/g' filename`
  1044.  
  1045. 346. Remove the last word from each line
  1046. `$ sed 's/\s\+\S$//' filename`
  1047.  
  1048. 347. Remove lines that contain digits
  1049. `$ sed '/[0-9]/d' filename`
  1050.  
  1051. 348. Replace text in a specific line range
  1052. `$ sed '10,20s/old_string/new_string/g' filename`
  1053.  
  1054. 349. Replace the nth word on each line
  1055. `$ sed 's/\(\(\S\s\)\{n-1\}\)\S/\1new_word/' filename`
  1056.  
  1057. 350. Print every third line
  1058. `$ sed -n 'n~3p' filename`
  1059.  
  1060. 351. Print all lines before a pattern match
  1061. `$ sed -n '1,/pattern/p' filename`
  1062.  
  1063. 352. Print all lines after a pattern match
  1064. `$ sed -n '/pattern/,$p' filename`
  1065.  
  1066. 353. Remove all spaces in a file
  1067. `$ sed 's/ //g' filename`
  1068.  
  1069. 354. Replace the first occurrence of a string only on the last line
  1070. `$ sed '$s/old_string/new_string/' filename`
  1071.  
  1072. 355. Delete lines from the beginning until a pattern match is found
  1073. `$ sed '/pattern/,$d' filename`
  1074.  
  1075. 356. Add a line at the end of a file
  1076. `$ sed -e '$a\New last line' filename`
  1077.  
  1078. 357. Replace string only in the first 5 lines
  1079. `$ sed '1,5s/old_string/new_string/g' filename`
  1080.  
  1081. 358. Replace the nth occurrence of a string in a line
  1082. `$ sed 's/old_string/new_string/n' filename`
  1083.  
  1084. 359. Replace a string but preserve the case of the original
  1085. `$ sed 's/old_string/\L&/g' filename`
  1086.  
  1087. 360. Print the first and last line of a file
  1088. `$ sed -n '1p; $p' filename`
  1089.  
  1090. 361. Replace a string on every line except for the first
  1091. `$ sed '1!s/old_string/new_string/g' filename`
  1092.  
  1093. 362. Insert a line after a specific line number
  1094. `$ sed 'n a\New content' filename`
  1095.  
  1096. 363. Replace string globally, ignoring case
  1097. `$ sed 's/old_string/new_string/I' filename`
  1098.  
  1099. 364. Print lines that start with a capital letter
  1100. `$ sed -n '/^[A-Z]/p' filename`
  1101.  
  1102. 365. Insert a line before a match and after a match
  1103. `$ sed '/pattern/{i\Before; a\After;}' filename`
  1104.  
  1105. 366. Remove trailing spaces from each line
  1106. `$ sed 's/[ \t]$//' filename`
  1107.  
  1108. 367. Replace all occurrences of a string except for the last one
  1109. `$ sed '$!s/old_string/new_string/g' filename`
  1110.  
  1111. 368. Replace the last word of each line
  1112. `$ sed 's/\(\S\)$/new_word/' filename`
  1113.  
  1114. 369. Print every even-numbered line
  1115. `$ sed -n 'n~2p' filename`
  1116.  
  1117. 370. Change the nth word of each line
  1118. `$ sed 's/\(\w\+\) \(\w\+\) \(\w\+\)/\1 new_word \3/' filename`
  1119.  
  1120. 371. Add a prefix to every line in the file
  1121. `$ sed 's/^/Prefix /' filename`
  1122.  
  1123. 372. Replace a string in lines that do not match a pattern
  1124. `$ sed '/pattern/!s/old_string/new_string/' filename`
  1125.  
  1126. 373. Remove all spaces in a line
  1127. `$ sed 's/ //g' filename`
  1128.  
  1129. 374. Print a line only if it contains more than 5 words
  1130. `$ sed -n '/\(\w\+\s\)\{5,\}/p' filename`
  1131.  
  1132. 375. Remove lines containing specific characters
  1133. `$ sed '/[#$@]/d' filename`
  1134.  
  1135. 376. Print lines between the first match and last match of a pattern
  1136. `$ sed -n '/start_pattern/,/end_pattern/p' filename`
  1137.  
  1138. 377. Insert a string after a specific word
  1139. `$ sed 's/\(pattern\)/\1 new_string/' filename`
  1140.  
  1141. 378. Remove lines from a specific pattern to the end of the file
  1142. `$ sed '/pattern/,$d' filename`
  1143.  
  1144. 379. Replace the first occurrence of a string in each line
  1145. `$ sed 's/old_string/new_string/' filename`
  1146.  
  1147. 380. Replace a word with a different one in lines containing a pattern
  1148. `$ sed '/pattern/s/old_word/new_word/' filename`
  1149.  
  1150. 381. Delete the first word from every line
  1151. `$ sed 's/^\S\s//' filename`
  1152.  
  1153. 382. Insert a line before a pattern match
  1154. `$ sed '/pattern/i\New line before match' filename`
  1155.  
  1156. 383. Print lines after the nth occurrence of a pattern
  1157. `$ sed -n '/pattern/{n;p}' filename`
  1158.  
  1159. 384. Add text after each line matching a pattern
  1160. `$ sed '/pattern/s/$/ New text/' filename`
  1161.  
  1162. 385. Print the lines before a specific pattern match
  1163. `$ sed -n '1,/pattern/p' filename`
  1164.  
  1165. 386. Replace a pattern only in the lines with more than 5 words
  1166. `$ sed '/\(\w\+\s\)\{5,\}/s/old_string/new_string/' filename`
  1167.  
  1168. 387. Replace the nth word in a specific line
  1169. `$ sed '5s/\(\w\+\) \(\w\+\)/\1 new_word/' filename`
  1170.  
  1171. 388. Remove all blank lines from a file
  1172. `$ sed '/^$/d' filename`
  1173.  
  1174. 389. Remove everything before a specific character
  1175. `$ sed 's/^[^pattern]//' filename`
  1176.  
  1177. 390. Add a suffix to every line in the file
  1178. `$ sed 's/$/Suffix/' filename`
  1179.  
  1180. 391. Print all lines from a specific match to the end of the file
  1181. `$ sed -n '/pattern/,$p' filename`
  1182.  
  1183. 392. Remove the first word and space in each line
  1184. `$ sed 's/^\S //' filename`
  1185.  
  1186. 393. Remove trailing white spaces at the end of the file
  1187. `$ sed 's/[ \t]$//' filename`
  1188.  
  1189. 394. Insert multiple lines after a pattern
  1190. `$ sed '/pattern/a\Line 1\nLine 2' filename`
  1191.  
  1192. 395. Replace only the last occurrence of a string in the file
  1193. `$ sed 's/old_string/new_string/g' filename | tac | sed 's/old_string/new_string/' | tac`
  1194.  
  1195. 396. Replace multiple spaces with a single space
  1196. `$ sed 's/ / /g' filename`
  1197.  
  1198. 397. Delete lines that are greater than 80 characters
  1199. `$ sed '/^.\{80\}/d' filename`
  1200.  
  1201. 398. Remove all digits from the file
  1202. `$ sed 's/[0-9]//g' filename`
  1203.  
  1204. 399. Print the lines after a specific string match
  1205. `$ sed -n '/start_pattern/,$p' filename`
  1206.  
  1207. 400. Remove lines matching a pattern
  1208. `$ sed '/pattern/d' filename`
  1209.  
  1210. 401. Add a line at the end of a specific line number
  1211. `$ sed '5a\New line' filename`
  1212.  
  1213. 402. Replace the second word in each line
  1214. `$ sed 's/\(\S\+\) \(\S\+\)/\1 new_word/' filename`
  1215.  
  1216. 403. Insert a new line after a pattern match
  1217. `$ sed '/pattern/a\New line after match' filename`
  1218.  
  1219. 404. Remove all lines containing only whitespace
  1220. `$ sed '/^[[:space:]]$/d' filename`
  1221.  
  1222. 405. Print only the last line of the file
  1223. `$ sed -n '$p' filename`
  1224.  
  1225. 406. Replace the first occurrence of a string on a specific line
  1226. `$ sed '5s/old_string/new_string/' filename`
  1227.  
  1228. 407. Remove everything before the first comma
  1229. `$ sed 's/^[^,],//' filename`
  1230.  
  1231. 408. Replace only the nth occurrence in a line
  1232. `$ sed 's/old_string/new_string/n' filename`
  1233.  
  1234. 409. Print all lines that contain a pattern
  1235. `$ sed -n '/pattern/p' filename`
  1236.  
  1237. 410. Replace the first 10 occurrences of a string
  1238. `$ sed 's/old_string/new_string/10' filename`
  1239.  
  1240. 411. Remove lines between two patterns
  1241. `$ sed '/start_pattern/,/end_pattern/d' filename`
  1242.  
  1243. 412. Replace spaces with underscores
  1244. `$ sed 's/ /_/g' filename`
  1245.  
  1246. 413. Remove leading spaces from each line
  1247. `$ sed 's/^[[:space:]]//' filename`
  1248.  
  1249. 414. Print all lines except the first 10
  1250. `$ sed '1,10d' filename`
  1251.  
  1252. 415. Delete all lines after the 10th line
  1253. `$ sed '10,$d' filename`
  1254.  
  1255. 416. Insert a line at the beginning of the file
  1256. `$ sed -i '1i\New first line' filename`
  1257.  
  1258. 417. Replace a string only in a specific line range
  1259. `$ sed '5,10s/old_string/new_string/g' filename`
  1260.  
  1261. 418. Replace a string only in the first match on each line
  1262. `$ sed 's/old_string/new_string/' filename`
  1263.  
  1264. 419. Remove the last character of every line
  1265. `$ sed 's/.$//' filename`
  1266.  
  1267. 420. Delete a specific word from all lines
  1268. `$ sed 's/word//g' filename`
  1269.  
  1270. 421. Replace commas with spaces
  1271. `$ sed 's/,/ /g' filename`
  1272.  
  1273. 422. Replace the nth character of every line
  1274. `$ sed 's/^\(.\{n\}\)./\1new_char/' filename`
  1275.  
  1276. 423. Delete lines that match a pattern, but keep their content
  1277. `$ sed '/pattern/{N; s/\n//;}' filename`
  1278.  
  1279. 424. Remove all lines starting with a specific character
  1280. `$ sed '/^#/d' filename`
  1281.  
  1282. 425. Insert a line at the end of the file
  1283. `$ sed -e '$a\New last line' filename`
  1284.  
  1285. 426. Replace the last occurrence of a word in the file
  1286. `$ sed 's/old_word/new_word/g' filename | tac | sed 's/old_word/new_word/' | tac`
  1287.  
  1288. 427. Print every other line starting from the first
  1289. `$ sed -n '1~2p' filename`
  1290.  
  1291. 428. Replace all uppercase letters with lowercase
  1292. `$ sed 's/[A-Z]/\L&/g' filename`
  1293.  
  1294. 429. Print every line that matches a pattern, and the next line after
  1295. `$ sed -n '/pattern/{n;p}' filename`
  1296.  
  1297. 430. Remove empty lines
  1298. `$ sed '/^$/d' filename`
  1299.  
  1300. 431. Replace a string on a specific line
  1301. `$ sed '5s/old_string/new_string/' filename`
  1302.  
  1303. 432. Replace a string only if it's on the first word of a line
  1304. `$ sed 's/^old_string/new_string/' filename`
  1305.  
  1306. 433. Replace every instance of a string, ignoring case
  1307. `$ sed 's/old_string/new_string/I' filename`
  1308.  
  1309. 434. Print lines between two patterns (inclusive)
  1310. `$ sed -n '/start_pattern/,/end_pattern/p' filename`
  1311.  
  1312. 435. Remove everything after a comma in each line
  1313. `$ sed 's/,.//' filename`
  1314.  
  1315. 436. Replace a word with its uppercase version
  1316. `$ sed 's/word/\U&/g' filename`
  1317.  
  1318. 437. Replace all dots with commas
  1319. `$ sed 's/\./,/g' filename`
  1320.  
  1321. 438. Remove all lines after a pattern match
  1322. `$ sed '/pattern/,$d' filename`
  1323.  
  1324. 439. Insert a line before every line with a number
  1325. `$ sed '/[0-9]/i\New line before numbers' filename`
  1326.  
  1327. 440. Replace all occurrences of a string with its reverse
  1328. `$ sed 's/old_string/'$(echo old_string | rev)'/g' filename`
  1329.  
  1330. 441. Replace only the first occurrence of a pattern in a line
  1331. `$ sed 's/pattern/new_string/' filename`
  1332.  
  1333. 442. Remove trailing tabs from each line
  1334. `$ sed 's/\t$//' filename`
  1335.  
  1336. 443. Print every line that doesn’t contain a specific pattern
  1337. `$ sed -n '/pattern/!p' filename`
  1338.  
  1339. 444. Print the last 10 lines
  1340. `$ sed -n '10,$p' filename`
  1341.  
  1342. 445. Replace the second occurrence of a string in a line
  1343. `$ sed 's/old_string/new_string/2' filename`
  1344.  
  1345. 446. Add a prefix to every line
  1346. `$ sed 's/^/Prefix /' filename`
  1347.  
  1348. 447. Print lines starting with a specific character
  1349. `$ sed -n '/^#/p' filename`
  1350.  
  1351. 448. Remove specific characters from a file
  1352. `$ sed 's/[[:punct:]]//g' filename`
  1353.  
  1354. 449. Replace multiple spaces with a single space
  1355. `$ sed 's/ / /g' filename`
  1356.  
  1357. 450. Replace all spaces with tabs
  1358. `$ sed 's/ /\t/g' filename`
  1359.  
  1360. 451. Change the case of each word
  1361. `$ sed 's/\w\+/\u&/g' filename`
  1362.  
  1363. 452. Remove lines that contain only digits
  1364. `$ sed '/^[0-9]$/d' filename`
  1365.  
  1366. 453. Add a string to the beginning of each line
  1367. `$ sed 's/^/New string /' filename`
  1368.  
  1369. 454. Remove all words starting with a specific letter
  1370. `$ sed 's/\b[aA]\w\b//g' filename`
  1371.  
  1372. 455. Replace a string globally only if it matches the whole line
  1373. `$ sed '/^old_string$/s/old_string/new_string/' filename`
  1374.  
  1375. 456. Insert text before a pattern
  1376. `$ sed '/pattern/i\New text before pattern' filename`
  1377.  
  1378. 457. Delete lines that are longer than 80 characters
  1379. `$ sed '/^.\{80\}/d' filename`
  1380.  
  1381. 458. Replace the nth word of every line with a string
  1382. `$ sed 's/\(\w\+\) \(\w\+\) \(\w\+\)/\1 new_word \3/' filename`
  1383.  
  1384. 459. Delete lines that don’t contain a pattern
  1385. `$ sed '/pattern/!d' filename`
  1386.  
  1387. 460. Remove characters from a specific position to the end of each line
  1388. `$ sed 's/^\(.\{n\}\)./\1/' filename`
  1389.  
  1390. 461. Replace first occurrence of a string in the last 5 lines
  1391. `$ sed '$-5,$s/old_string/new_string/' filename`
  1392.  
  1393. 462. Delete lines from the first occurrence of a pattern to the last line
  1394. `$ sed '/pattern/,$d' filename`
  1395.  
  1396. 463. Print lines that do not match a pattern
  1397. `$ sed -n '/pattern/!p' filename`
  1398.  
  1399. 464. Replace all hyphens with spaces
  1400. `$ sed 's/-/ /g' filename`
  1401.  
  1402. 465. Print only lines starting with a specific word
  1403. `$ sed -n '/^start_word/p' filename`
  1404.  
  1405. 466. Remove blank lines with tabs or spaces
  1406. `$ sed '/^[[:space:]]$/d' filename`
  1407.  
  1408. 467. Print a range of lines starting from a specific line
  1409. `$ sed -n '5,15p' filename`
  1410.  
  1411. 468. Replace the nth occurrence of a pattern in the whole file
  1412. `$ sed 's/old_string/new_string/n' filename`
  1413.  
  1414. 469. Remove lines that contain a pattern and their surrounding lines
  1415. `$ sed '/pattern/{N; d;}' filename`
  1416.  
  1417. 470. Replace a string on lines containing another string
  1418. `$ sed '/pattern/s/old_string/new_string/' filename`
  1419.  
  1420. 471. Replace the first word in a line with another word
  1421. `$ sed 's/^\w\+/new_word/' filename`
  1422.  
  1423. 472. Remove lines with the pattern at the start and end of the line
  1424. `$ sed '/^start_pattern$/d' filename`
  1425.  
  1426. 473. Remove the last word of the last line
  1427. `$ sed '$s/\s\+\S$//' filename`
  1428.  
  1429. 474. Replace tabs with a specific number of spaces
  1430. `$ sed 's/\t/ /g' filename`
  1431.  
  1432. 475. Add a suffix to the end of every line
  1433. `$ sed 's/$/ New suffix/' filename`
  1434.  
  1435. 476. Replace all numbers with a word
  1436. `$ sed 's/[0-9]\+/word/g' filename`
  1437.  
  1438. 477. Replace the nth occurrence of a string on specific lines
  1439. `$ sed '5,10s/old_string/new_string/n' filename`
  1440.  
  1441. 478. Print all lines except the ones starting with a specific letter
  1442. `$ sed '/^A/!p' filename`
  1443.  
  1444. 479. Delete all lines that contain only alphabets
  1445. `$ sed '/^[A-Za-z]$/d' filename`
  1446.  
  1447. 480. Insert a line after every pattern match
  1448. `$ sed '/pattern/a\New line after match' filename`
  1449.  
  1450. 481. Replace a string globally only for even-numbered lines
  1451. `$ sed 'n~2s/old_string/new_string/g' filename`
  1452.  
  1453. 482. Remove all non-alphabetic characters from the file
  1454. `$ sed 's/[^A-Za-z]//g' filename`
  1455.  
  1456. 483. Print every line except the ones with specific characters
  1457. `$ sed '/[#$@]/!p' filename`
  1458.  
  1459. 484. Remove all commas and replace them with a space
  1460. `$ sed 's/,/ /g' filename`
  1461.  
  1462. 485. Delete all lines with the string "delete"
  1463. `$ sed '/delete/d' filename`
  1464.  
  1465. 486. Remove extra spaces and convert them to a single space
  1466. `$ sed 's/ / /g' filename`
  1467.  
  1468. 487. Replace every tab with four spaces
  1469. `$ sed 's/\t/ /g' filename`
  1470.  
  1471. 488. Delete lines that match a pattern but add a comment
  1472. `$ sed '/pattern/{s/.$/#Commented line/;}' filename`
  1473.  
  1474. 489. Replace all words with "word"
  1475. `$ sed 's/\w\+/word/g' filename`
  1476.  
  1477. 490. Change case of a word globally
  1478. `$ sed 's/old_word/\U&/g' filename`
  1479.  
  1480. 491. Add a line before every pattern match
  1481. `$ sed '/pattern/i\New line before match' filename`
  1482.  
  1483. 492. Delete the first word from each line
  1484. `$ sed 's/^\S\s//' filename`
  1485.  
  1486. 493. Replace every occurrence of a string in a specific column
  1487. `$ sed 's/\(\S\s\)\{n\}old_string/new_string/' filename`
  1488.  
  1489. 494. Delete all empty lines from a file
  1490. `$ sed '/^$/d' filename`
  1491.  
  1492. 495. Remove all instances of a string from a file
  1493. `$ sed 's/old_string//g' filename`
  1494.  
  1495. 496. Print only lines matching a specific word and add a suffix
  1496. `$ sed '/pattern/s/$/ suffix/' filename`
  1497.  
  1498. 497. Delete lines that are over a certain length
  1499. `$ sed '/^.\{80\}/d' filename`
  1500.  
  1501. 498. Replace a string and add it to the end of the line
  1502. `$ sed 's/old_string/& appended_string/' filename`
  1503.  
  1504. 499. Replace all digits with a placeholder
  1505. `$ sed 's/[0-9]/X/g' filename`
  1506.  
  1507. 500. Remove lines that start with a specific character #
  1508. `$ sed '/^#/'d filename`
  1509.  
  1510. 501. Print lines start with specific character #
  1511. `$ sed -n '/^#/p' filename`
  1512.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement