Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Top 500 Useful Sed commands
- ##################################################################
- Join our telegram channel for more : https://t.me/LinuxClassesEFXTv
- ##################################################################
- Certainly! Here's a list of `sed` commands you may not know
- 1. Substitute a string in a file
- `$ sed 's/old_string/new_string/' filename`
- 2. Substitute a string globally (across the entire line)
- `$ sed 's/old_string/new_string/g' filename`
- 3. Substitute a string in a specific line number
- `$ sed '3s/old_string/new_string/' filename`
- 4. Substitute a string in a range of line numbers
- `$ sed '2,5s/old_string/new_string/' filename`
- 5. Delete a specific line
- `$ sed '3d' filename`
- 6. Delete a range of lines
- `$ sed '2,5d' filename`
- 7. Print only specific lines
- `$ sed -n '2,5p' filename`
- 8. Print all lines except a range of lines
- `$ sed '2,5!p' filename`
- 9. Delete empty lines
- `$ sed '/^$/d' filename`
- 10. Remove leading whitespaces
- `$ sed 's/^[ \t]//' filename`
- 11. Remove trailing whitespaces
- `$ sed 's/[ \t]$//' filename`
- 12. Remove both leading and trailing whitespaces
- `$ sed 's/^[ \t]//;s/[ \t]$//' filename`
- 13. Substitute string with case-insensitivity
- `$ sed 's/old_string/new_string/Ig' filename`
- 14. Replace only the first occurrence of a string
- `$ sed '0,/old_string/s//new_string/' filename`
- 15. Substitute on lines matching a pattern
- `$ sed '/pattern/s/old_string/new_string/' filename`
- 16. Append a line after a specific line
- `$ sed '3a\This is a new line' filename`
- 17. Prepend a line before a specific line
- `$ sed '3i\This is a new line' filename`
- 18. Change a line entirely
- `$ sed '3c\This is the new line' filename`
- 19. Remove lines matching a pattern
- `$ sed '/pattern/d' filename`
- 20. Replace the nth occurrence of a string
- `$ sed 's/old_string/new_string/2' filename`
- 21. Replace text and write output to a new file
- `$ sed 's/old_string/new_string/g' filename > newfile`
- 22. Save the modified file in-place
- `$ sed -i 's/old_string/new_string/g' filename`
- 23. Create a backup when modifying the file in-place
- `$ sed -i.bak 's/old_string/new_string/g' filename`
- 24. Remove a line that starts with a specific pattern
- `$ sed '/^pattern/d' filename`
- 25. Remove a line that ends with a specific pattern
- `$ sed '/pattern$/d' filename`
- 26. Replace a word with another word in a file
- `$ sed 's/old_word/new_word/g' filename`
- 27. Escape special characters in `sed`
- `$ sed 's/\//\\\//g' filename`
- 28. Insert a line after every line containing a pattern
- `$ sed '/pattern/a\This is the new line' filename`
- 29. Replace every nth word in a line
- `$ sed 's/\(\w\+\) \(\w\+\)/\1 replaced \2/' filename`
- 30. Remove specific columns from a text
- `$ sed 's/\([^ ]\) \([^ ]\) /\1 /' filename`
- 31. Remove all non-alphanumeric characters
- `$ sed 's/[^a-zA-Z0-9]//g' filename`
- 32. Insert text at the beginning of every line
- `$ sed 's/^/Text at start /' filename`
- 33. Insert text at the end of every line
- `$ sed 's/$/ Text at end/' filename`
- 34. Replace only lines that contain a certain string
- `$ sed '/pattern/s/old_string/new_string/g' filename`
- 35. Remove all digits from a file
- `$ sed 's/[0-9]//g' filename`
- 36. Replace newline with a space
- `$ sed ':a;N;$!ba;s/\n/ /g' filename`
- 37. Show line numbers with the output
- `$ sed = filename | sed 'N;s/\n/ /'`
- 38. Print specific lines by line number
- `$ sed -n '5p' filename`
- 39. Replace multiple spaces with a single space
- `$ sed 's/ \+/ /g' filename`
- 40. Show the line before and after the match
- `$ sed -n '/pattern/{=;p;}' filename`
- 41. Extract lines that match a pattern
- `$ sed -n '/pattern/p' filename`
- 42. Add line numbers to each line
- `$ sed = filename | sed 'N;s/\n/\t/'`
- 43. Remove all uppercase letters
- `$ sed 's/[A-Z]//g' filename`
- 44. Replace the first word in each line
- `$ sed 's/^\w\+/new_word/' filename`
- 45. Replace the last word in each line
- `$ sed 's/\w\+$/new_word/' filename`
- 46. Delete lines between two patterns
- `$ sed '/start_pattern/,/end_pattern/d' filename`
- 47. Replace a string in the file without case sensitivity
- `$ sed 's/old_string/new_string/I' filename`
- 48. Substitute with special characters
- `$ sed 's/[0-9]/#/g' filename`
- 49. Print the line before the matching pattern
- `$ sed -n '/pattern/{x;p;x;}' filename`
- 50. Replace all punctuation with spaces
- `$ sed 's/[[:punct:]]/ /g' filename`
- 51. Add a line after the last line
- `$ sed '$a\This is the last line' filename`
- 52. Remove the first line from the file
- `$ sed '1d' filename`
- 53. Remove the last line from the file
- `$ sed '$d' filename`
- 54. Replace first character in each line
- `$ sed 's/^./new_char/' filename`
- 55. Replace last character in each line
- `$ sed 's/.$/new_char/' filename`
- 56. Add text before a matching line
- `$ sed '/pattern/i\Text before pattern' filename`
- 57. Replace the nth occurrence of a pattern on all lines
- `$ sed 's/pattern/replacement/3' filename`
- 58. Replace pattern with a newline
- `$ sed 's/pattern/\n/g' filename`
- 59. Remove all comments (lines starting with `#`)
- `$ sed '/^#/d' filename`
- 60. Remove lines with a specific word
- `$ sed '/word/d' filename`
- 61. Display lines containing a specific pattern
- `$ sed -n '/pattern/p' filename`
- 62. Remove blank lines
- `$ sed '/^\s$/d' filename`
- 63. Replace string only on lines that don't match another pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 64. Substitute a string only on the first line
- `$ sed '1s/old_string/new_string/' filename`
- 65. Add a new line after every match
- `$ sed '/pattern/a\New line after match' filename`
- 66. Add a new line before every match
- `$ sed '/pattern/i\New line before match' filename`
- 67. Insert a line at a specific line number
- `$ sed '3i\Insert line here' filename`
- 68. Delete lines matching a pattern globally
- `$ sed '/pattern/d' filename`
- 69. Replace specific word with a variable
- `$ sed "s/old_word/$variable/" filename`
- 70. Replace string globally across multiple files
- `$ sed -i 's/old_string/new_string/g' .txt`
- 71. Print lines that don’t match a pattern
- `$ sed -n '/pattern/!p' filename`
- 72. Delete lines matching multiple patterns
- `$ sed '/pattern1\|pattern2/d' filename`
- 73. Replace a word with multiple spaces
- `$ sed 's/word/ /g' filename`
- 74. Remove all spaces from a file
- `$ sed 's/ //g' filename`
- 75. Replace a string in the middle of a line
- `$ sed 's/middle_string/new_string/' filename`
- 76. Add a prefix to every line
- `$ sed 's/^/Prefix /' filename`
- 77. Add a suffix to every line
- `$ sed 's/$/ Suffix/' filename`
- 78. Remove everything after the first space on each line
- `$ sed 's/ .$//' filename`
- 79. Remove everything before the first space on each line
- `$ sed 's/^[^ ] //' filename`
- 80. Remove a word in a sentence
- `$ sed 's/\bword\b//g' filename`
- 81. Convert lowercase to uppercase
- `$ sed 's/./\U&/' filename`
- 82. Convert uppercase to lowercase
- `$ sed 's/./\L&/' filename`
- 83. Delete lines from a specific line number to the end of the file
- `$ sed '3,$d' filename`
- 84. Delete every nth line
- `$ sed '0~3d' filename`
- 85. Replace a string across multiple files in a directory
- `$ sed -i 's/old_string/new_string/g' /path/to/files/.txt`
- 86. Match words that start with a specific character
- `$ sed 's/\bA\w/new_string/g' filename`
- 87. Replace a word globally and display result
- `$ sed -n 's/old_word/new_word/gp' filename`
- 88. Print the first line only
- `$ sed -n '1p' filename`
- 89. Print the last line only
- `$ sed -n '$p' filename`
- 90. Print the nth line only
- `$ sed -n 'n p' filename`
- 91. Replace the string using a regex pattern
- `$ sed 's/\b[a-zA-Z]\{5\}\b/replacement/g' filename`
- 92. Replace a string but exclude a specific pattern
- `$ sed '/exclude_pattern/!s/old_string/new_string/' filename`
- 93. Count occurrences of a pattern
- `$ sed -n '/pattern/{s//&/g;p}' filename | wc -l`
- 94. Add a suffix before the last character
- `$ sed 's/\(.\)/\1Suffix/' filename`
- 95. Insert a line before the last line
- `$ sed '$i\Line before last' filename`
- 96. Replace the first word of every line with a specific word
- `$ sed 's/^\w\+/new_word/' filename`
- 97. Add a line after the first match of a pattern
- `$ sed '/pattern/a\New line after first match' filename`
- 98. Remove specific words from each line
- `$ sed 's/\bword1\b//g; s/\bword2\b//g' filename`
- 99. Remove specific pattern and its following content
- `$ sed 's/pattern.$//' filename`
- 100. Insert a line after a specific pattern match
- `$ sed '/pattern/a\Insert line after match' filename`
- 101. Replace only the first line in the file
- `$ sed '1s/old_string/new_string/' filename`
- 102. Remove all vowels from a file
- `$ sed 's/[aeiouAEIOU]//g' filename`
- 103. Replace all digits with `#`
- `$ sed 's/[0-9]/#/g' filename`
- 104. Add a new line after every nth line
- `$ sed 'n a\New line after nth' filename`
- 105. Replace a string and print line numbers
- `$ sed -n 's/old_string/new_string/p' filename`
- 106. Remove the first and last character from each line
- `$ sed 's/^.\(.\).$/\1/' filename`
- 107. Remove blank lines and lines with only spaces
- `$ sed '/^[[:space:]]$/d' filename`
- 108. Replace only in lines that do not contain a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 109. Replace the last occurrence of a string
- `$ sed 's/\(.\)old_string/\1new_string/' filename`
- 110. Print only lines matching a pattern
- `$ sed -n '/pattern/p' filename`
- 111. Print lines starting with a specific character
- `$ sed -n '/^A/p' filename`
- 112. Remove the first occurrence of a string
- `$ sed 's/old_string//' filename`
- 113. Replace a pattern using a variable
- `$ sed "s/$old_string/$new_string/" filename`
- 114. Print line numbers along with content
- `$ sed '=' filename | sed 'N;s/\n/\t/'`
- 115. Replace multiple spaces with a single space
- `$ sed 's/ / /g' filename`
- 116. Convert all spaces to underscores
- `$ sed 's/ /_/g' filename`
- 117. Escape forward slashes
- `$ sed 's/\//\\\//g' filename`
- 118. Remove non-alphanumeric characters
- `$ sed 's/[^a-zA-Z0-9]//g' filename`
- 119. Replace newlines with a space
- `$ sed ':a;N;$!ba;s/\n/ /g' filename`
- 120. Insert content before a specific string match
- `$ sed '/pattern/i\Insert before pattern' filename`
- 121. Replace the nth character in a line
- `$ sed 's/\(.\{n\}\).\{1\}/\1replacement/' filename`
- 122. Print lines that end with a specific string
- `$ sed -n '/pattern$/p' filename`
- 123. Print the first n lines
- `$ sed -n '1,5p' filename`
- 124. Remove every nth line
- `$ sed 'n!d' filename`
- 125. Add text to the beginning of the file
- `$ sed -i '1s/^/Text at the start of the file\n/' filename`
- 126. Replace a string and ignore case sensitivity
- `$ sed 's/old_string/new_string/I' filename`
- 127. Remove lines containing a pattern from a specific file
- `$ sed '/pattern/d' filename`
- 128. Substitute using extended regular expressions
- `$ sed -r 's/(pattern)/replacement/' filename`
- 129. Replace text in a specific column
- `$ sed 's/^\([^\t]\)\t\([^\t]\)$/\1\tnew_text/' filename`
- 130. Replace all spaces with hyphens
- `$ sed 's/ /-/g' filename`
- 131. Replace all instances of a string and show result
- `$ sed 's/old_string/new_string/gp' filename`
- 132. Change the case of every word
- `$ sed 's/\(.\)/\U&/' filename`
- 133. Replace all punctuation marks
- `$ sed 's/[[:punct:]]/ /g' filename`
- 134. Replace word at the beginning of each line
- `$ sed 's/^word/new_word/' filename`
- 135. Reverse every line in a file
- `$ sed 's/\(.\)/\1/' filename | rev`
- 136. Remove empty lines but not lines with spaces
- `$ sed '/^[[:space:]]$/d' filename`
- 137. Print lines with a specific number of words
- `$ sed -n '/\w\{5\}/p' filename`
- 138. Change words between specific tags
- `$ sed 's/<tag>\(.\)<\/tag>/<tag>new_text<\/tag>/' filename`
- 139. Replace first letter of each word with a capital
- `$ sed 's/\b\(.\)/\U\1/g' filename`
- 140. Replace multiple spaces with a single space using a regex
- `$ sed 's/\s\+/ /g' filename`
- 141. Substitute multiple occurrences on multiple lines
- `$ sed ':a;N;$!ba;s/old_string/new_string/g' filename`
- 142. Remove text between two patterns
- `$ sed 's/pattern1.pattern2//g' filename`
- 143. Add a blank line between lines matching a pattern
- `$ sed '/pattern/a\ ' filename`
- 144. Display content of specific line and its preceding line
- `$ sed -n '2p; 1p' filename`
- 145. Search and replace using `-e` (expression)
- `$ sed -e 's/old/new/' filename`
- 146. Replace multiple patterns on one line
- `$ sed 's/old1/new1; s/old2/new2/' filename`
- 147. Reverse the contents of a file
- `$ sed 's/./&/' filename | rev`
- 148. Replace the nth instance of a word in a string
- `$ sed 's/word/new_word/n' filename`
- 149. Replace string but print before and after
- `$ sed 's/old_string/new_string/p' filename`
- 150. Replace specific pattern with the content from a file
- `$ sed 's/pattern/$(cat replacement_file)/' filename`
- 151. Replace the nth occurrence of a string only
- `$ sed 's/old_string/new_string/3' filename`
- 152. Remove leading spaces from each line
- `$ sed 's/^[ \t]//' filename`
- 153. Remove trailing spaces from each line
- `$ sed 's/[ \t]$//' filename`
- 154. Remove all spaces
- `$ sed 's/ //g' filename`
- 155. Replace string in a case-insensitive manner
- `$ sed 's/old_string/new_string/I' filename`
- 156. Delete lines matching a regular expression
- `$ sed '/pattern/d' filename`
- 157. Print the lines matching a specific pattern
- `$ sed -n '/pattern/p' filename`
- 158. Replace all occurrences of a pattern across multiple files
- `$ sed -i 's/old_string/new_string/g' .txt`
- 159. Add a prefix to all lines
- `$ sed 's/^/Prefix: /' filename`
- 160. Remove empty lines
- `$ sed '/^$/d' filename`
- 161. Remove lines containing a specific word
- `$ sed '/word/d' filename`
- 162. Add a new line before every line that matches a pattern
- `$ sed '/pattern/i\New line before match' filename`
- 163. Substitute the last word of every line
- `$ sed 's/\w$/new_word/' filename`
- 164. Remove the first word from each line
- `$ sed 's/^\w\+ //' filename`
- 165. Remove all non-alphanumeric characters
- `$ sed 's/[^a-zA-Z0-9]//g' filename`
- 166. Remove all digits from a file
- `$ sed 's/[0-9]//g' filename`
- 167. Add text to the end of each line
- `$ sed 's/$/ appended text/' filename`
- 168. Convert all letters to uppercase
- `$ sed 's/./\U&/' filename`
- 169. Convert all letters to lowercase
- `$ sed 's/./\L&/' filename`
- 170. Add line numbers to the beginning of each line
- `$ sed = filename | sed 'N;s/\n/\t/'`
- 171. Print only the nth line
- `$ sed -n 'n p' filename`
- 172. Remove the first and last character of each line
- `$ sed 's/^.\(.\).$/\1/' filename`
- 173. Remove duplicate lines
- `$ sed '$!N; /^\(.\)\n\1$/!P; D' filename`
- 174. Replace spaces between words with underscores
- `$ sed 's/ /_/g' filename`
- 175. Replace multiple spaces with a single space
- `$ sed 's/ / /g' filename`
- 176. Replace text in lines that do not match a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 177. Replace text only in the first line
- `$ sed '1s/old_string/new_string/' filename`
- 178. Show the line before a match
- `$ sed -n '/pattern/{x;p;x;}' filename`
- 179. Remove all spaces at the beginning of each line
- `$ sed 's/^[ \t]//' filename`
- 180. Insert text after each line containing a pattern
- `$ sed '/pattern/a\Inserted text after match' filename`
- 181. Insert text before each line containing a pattern
- `$ sed '/pattern/i\Inserted text before match' filename`
- 182. Match a line that starts with a specific word
- `$ sed -n '/^word/p' filename`
- 183. Match a line that ends with a specific word
- `$ sed -n '/word$/p' filename`
- 184. Remove a specific word from the file
- `$ sed 's/\bword\b//g' filename`
- 185. Remove the last line in a file
- `$ sed '$d' filename`
- 186. Remove the first line in a file
- `$ sed '1d' filename`
- 187. Print lines containing a word but not matching a pattern
- `$ sed -n '/word/!p' filename`
- 188. Replace a string and display result on the terminal
- `$ sed 's/old_string/new_string/gp' filename`
- 189. Replace text in a specific line number and print it
- `$ sed '3s/old_string/new_string/p' filename`
- 190. Print the first 10 lines of the file
- `$ sed -n '1,10p' filename`
- 191. Replace a word with a newline character
- `$ sed 's/old_word/\n/g' filename`
- 192. Show lines with a specific pattern but exclude others
- `$ sed -n '/pattern/{p;}' filename`
- 193. Count number of occurrences of a word in a file
- `$ sed -n 's/word/&/gp' filename | wc -l`
- 194. Change a string with a random value
- `$ sed 's/old_string/$(shuf -n 1 value_list)/' filename`
- 195. Print only lines that match the regular expression
- `$ sed -n '/pattern/p' filename`
- 196. Replace the first character of the line
- `$ sed 's/^./new_char/' filename`
- 197. Replace the last character of the line
- `$ sed 's/.$/new_char/' filename`
- 198. Replace the nth occurrence of a string
- `$ sed 's/old_string/new_string/2' filename`
- 199. Print the last line of the file
- `$ sed -n '$p' filename`
- 200. Print lines after a match
- `$ sed -n '/pattern/,$p' filename`
- 201. Replace all occurrences of a string in the file
- `$ sed 's/old_string/new_string/g' filename`
- 202. Replace multiple words on each line
- `$ sed 's/old1/new1; s/old2/new2/g' filename`
- 203. Change case of every word in a line
- `$ sed 's/\(.\)/\U&/' filename`
- 204. Remove a specific line from the file
- `$ sed '3d' filename`
- 205. Replace each occurrence of a word with different values
- `$ sed 's/word1/replacement1/; s/word2/replacement2/' filename`
- 206. Replace a word but keep spaces intact
- `$ sed 's/\bword\b/&/' filename`
- 207. Delete lines containing the word 'pattern'
- `$ sed '/pattern/d' filename`
- 208. Print lines matching a pattern with line numbers
- `$ sed -n '/pattern/{=; p;}' filename`
- 209. Replace the word only on the second line
- `$ sed '2s/old_word/new_word/' filename`
- 210. Remove every other line from the file
- `$ sed 'n!d' filename`
- 211. Print the file in reverse order
- `$ sed '1!G;h;$!d' filename`
- 212. Match a line that does not start with a specific word
- `$ sed -n '/^[^word]/p' filename`
- 213. Add a suffix to every line
- `$ sed 's/$/Suffix/' filename`
- 214. Substitute spaces with commas
- `$ sed 's/ /,/g' filename`
- 215. Add a prefix to every line that does not match a pattern
- `$ sed '/pattern/!s/^/Prefix: /' filename`
- 216. Remove all digits from the file
- `$ sed 's/[0-9]//g' filename`
- 217. Insert content after specific line number
- `$ sed '3a\New content' filename`
- 218. Replace a specific string and ignore case
- `$ sed 's/old_string/new_string/I' filename`
- 219. Print the nth line and the next one
- `$ sed -n 'n,+1p' filename`
- 220. Replace a pattern with the content from another file
- `$ sed 's/pattern/$(<file)/' filename`
- 221. Remove all lines containing only whitespace
- `$ sed '/^[[:space:]]$/d' filename`
- 222. Add a line before every occurrence of a pattern
- `$ sed '/pattern/i\New line before match' filename`
- 223. Substitute a string but only on the last line
- `$ sed '$s/old_string/new_string/' filename`
- 224. Remove text after the first occurrence of a string
- `$ sed 's/old_string.$//' filename`
- 225. Match lines with a specific number of characters
- `$ sed -n '/^.\{5\}$/p' filename`
- 226. Remove all special characters
- `$ sed 's/[^a-zA-Z0-9]//g' filename`
- 227. Replace all occurrences of a string in the first 10 lines
- `$ sed '1,10s/old_string/new_string/g' filename`
- 228. Delete lines with a specific number of characters
- `$ sed '/^.\{10\}$/d' filename`
- 229. Remove comments from shell scripts
- `$ sed '/^#/d' filename`
- 230. Insert content at the beginning of the file
- `$ sed -i '1s/^/New content\n/' filename`
- 231. Change string in specific columns
- `$ sed 's/\(\w\+\) \(\w\+\)/\1 \2 replacement/' filename`
- 232. Substitute using backreference
- `$ sed 's/\(pattern\)/\1 replacement/' filename`
- 233. Remove trailing spaces from each line
- `$
- sed 's/[ \t]$//' filename`
- 234. Replace all occurrences of a string in the first 5 lines
- `$ sed '1,5s/old_string/new_string/g' filename`
- 235. Show the lines from 10 to 20
- `$ sed -n '10,20p' filename`
- 236. Replace text globally excluding a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 237. Remove a range of lines from a file
- `$ sed '3,7d' filename`
- 238. Count the number of occurrences of a pattern
- `$ sed -n 's/pattern/&/gp' filename | wc -l`
- 239. Insert a line at the end of the file
- `$ sed -e '\$a\Last line' filename`
- 240. Delete all lines with a specific length
- `$ sed '/^.\{5\}$/d' filename`
- 241. Replace the first occurrence of a string on the last line only
- `$ sed '$s/old_string/new_string/' filename`
- 242. Remove all non-alphanumeric characters except space
- `$ sed 's/[^a-zA-Z0-9 ]//g' filename`
- 243. Replace string in a specific line by line number
- `$ sed '5s/old_string/new_string/' filename`
- 244. Substitute the first word on each line
- `$ sed 's/^\w\+/new_word/' filename`
- 245. Replace a string in lines that do not match a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 246. Find and print line numbers that match a pattern
- `$ sed -n '/pattern/=' filename`
- 247. Remove empty lines and lines with only spaces
- `$ sed '/^[[:space:]]$/d' filename`
- 248. Append a line after every line that matches a pattern
- `$ sed '/pattern/a\New appended line' filename`
- 249. Replace a string in lines before a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 250. Replace first occurrence of a string on specific line range
- `$ sed '1,5s/old_string/new_string/' filename`
- 251. Remove multiple spaces between words
- `$ sed 's/ / /g' filename`
- 252. Add a suffix to every line that matches a pattern
- `$ sed '/pattern/s/$/ suffix/' filename`
- 253. Print lines after a match and include the matched line
- `$ sed -n '/pattern/,$p' filename`
- 254. Replace all vowels with another character
- `$ sed 's/[aeiouAEIOU]/x/g' filename`
- 255. Print lines starting with a number
- `$ sed -n '/^[0-9]/p' filename`
- 256. Replace a string only in the first 10 lines
- `$ sed '1,10s/old_string/new_string/' filename`
- 257. Print lines that do not contain a pattern
- `$ sed -n '/pattern/!p' filename`
- 258. Replace space with newline after each word
- `$ sed 's/ /\'$'\n/g' filename`
- 259. Print lines before a specific pattern
- `$ sed -n '1,/pattern/p' filename`
- 260. Replace only the first occurrence of a pattern in the file
- `$ sed '0,/old_string/s//new_string/' filename`
- 261. Print the first line of a file
- `$ sed -n '1p' filename`
- 262. Remove the second column from a file
- `$ sed 's/\([^ ] [^ ]\) \(.\)/\1/' filename`
- 263. Insert a line before every line that contains a string
- `$ sed '/pattern/i\New line before match' filename`
- 264. Replace the nth word in each line
- `$ sed 's/\(\(\S\s\)\{n-1\}\)\S/\1new_word/' filename`
- 265. Delete lines that start with a specific word
- `$ sed '/^word/d' filename`
- 266. Remove all lines except the ones containing a pattern
- `$ sed '/pattern/!d' filename`
- 267. Replace only the last occurrence of a string in the file
- `$ sed 's/old_string/new_string/g' filename | tac | sed 's/old_string/new_string/' | tac`
- 268. Insert a new line at the beginning of a file
- `$ sed -i '1i\New beginning line' filename`
- 269. Replace specific word from each line by skipping lines that match a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 270. Add a prefix to every line
- `$ sed 's/^/Prefix /' filename`
- 271. Add a suffix to lines containing a specific pattern
- `$ sed '/pattern/s/$/ suffix/' filename`
- 272. Print the last n lines of the file
- `$ sed -n 'n,$p' filename`
- 273. Find all occurrences of a pattern and print lines containing them
- `$ sed -n '/pattern/p' filename`
- 274. Insert a new line after the first occurrence of a pattern
- `$ sed '/pattern/a\New line after first match' filename`
- 275. Replace the nth occurrence in a line
- `$ sed 's/old_string/new_string/n' filename`
- 276. Reverse the order of lines in a file
- `$ sed '1!G;h;$!d' filename`
- 277. Replace the nth occurrence of a string across multiple lines
- `$ sed '0,/old_string/s//new_string/' filename`
- 278. Replace a string across lines matching a pattern
- `$ sed '/pattern/ {s/old_string/new_string/}' filename`
- 279. Remove characters until the first space
- `$ sed 's/^[^ ] //' filename`
- 280. Replace the string on lines that do not match a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 281. Print the first 10 lines of a file
- `$ sed -n '1,10p' filename`
- 282. Remove all lines except the ones starting with a pattern
- `$ sed '/^pattern/!d' filename`
- 283. Replace only the first word in each line
- `$ sed 's/^\w\+/new_word/' filename`
- 284. Replace a string but preserve the case of the original
- `$ sed 's/old_string/\L&/g' filename`
- 285. Remove everything after the first comma in each line
- `$ sed 's/,.//' filename`
- 286. Remove characters after the nth character in a line
- `$ sed 's/\(.\{n\}\)./\1/' filename`
- 287. Remove spaces and tabs before and after the content
- `$ sed 's/^[[:space:]]//;s/[[:space:]]$//' filename`
- 288. Delete lines containing a pattern and print the result
- `$ sed '/pattern/d' filename`
- 289. Append a string at the end of the nth line
- `$ sed 'n s/$/ appended_text/' filename`
- 290. Replace the nth occurrence of a pattern in the file
- `$ sed 'n s/old_string/new_string/' filename`
- 291. Print a specific line range from the file
- `$ sed -n '10,20p' filename`
- 292. Remove everything after the first match of a string in each line
- `$ sed 's/\(.pattern\)./\1/' filename`
- 293. Remove all but the nth occurrence of a word in the line
- `$ sed 's/\(.old_string\)\(old_string\)/\1new_string/' filename`
- 294. Replace the first word with a string
- `$ sed 's/^\w\+/new_word/' filename`
- 295. Change the last character of each line
- `$ sed 's/.$/new_char/' filename`
- 296. Remove lines containing only digits
- `$ sed '/^[0-9]$/d' filename`
- 297. Replace words of length 4 with a string
- `$ sed 's/\b\w\{4\}\b/new_string/g' filename`
- 298. Replace all occurrences in the first 3 lines
- `$ sed '1,3s/old_string/new_string/g' filename`
- 299. Replace text only on lines that do not contain a word
- `$ sed '/\bword\b/!s/old_string/new_string/' filename`
- 300. Insert a line after a specific pattern in the file
- `$ sed '/pattern/a\New line after match' filename`
- 301. Remove trailing spaces and tabs in the entire file
- `$ sed 's/[ \t]$//' filename`
- 302. Print the lines before a specific pattern
- `$ sed -n '1,/pattern/p' filename`
- 303. Remove the last character in each line
- `$ sed 's/.$//' filename`
- 304. Insert a new line at the end of a file
- `$ sed -e '$a\New last line' filename`
- 305. Replace nth occurrence of a string and print it
- `$ sed 'n s/old_string/new_string/' filename`
- 306. Remove lines containing a specific pattern
- `$ sed '/pattern/d' filename`
- 307. Change the last occurrence of a word in a file
- `$ sed 's/old_string/new_string/2' filename`
- 308. Print lines that match a specific string pattern
- `$ sed -n '/pattern/p' filename`
- 309. Print lines matching a specific word and add numbering
- `$ sed -n '/pattern/=' filename`
- 310. Add a line at the start of the file
- `$ sed '1i\New first line' filename`
- 311. Replace the first occurrence of a pattern on the last line
- `$ sed '$s/old_string/new_string/' filename`
- 312. Replace a string and only print the result
- `$ sed 's/old_string/new_string/p' filename`
- 313. Replace text globally but ignore case
- `$ sed 's/old_string/new_string/I' filename`
- 314. Replace each word in a line with a specific string
- `$ sed 's/\w\+/new_string/g' filename`
- 315. Replace every word that is longer than 3 characters
- `$ sed 's/\b\w\{4,\}\b/new_word/g' filename`
- 316. Insert content after a specific line number
- `$ sed 'n a\New content' filename`
- 317. Print the lines before and after a match
- `$ sed -n '/pattern/{x; p; x;}' filename`
- 318. Replace the word with a string only in the first 5 lines
- `$ sed '1,5s/old_string/new_string/' filename`
- 319. Remove the first occurrence of a string in the entire file
- `$ sed 's/old_string//' filename`
- 320. Change a string in the first 5 lines but only print it
- `$ sed -n '1,5s/old_string/new_string/p' filename`
- 321. Replace only the first instance of a string
- `$ sed '0,/old_string/s//new_string/' filename`
- 322. Print all lines except the last one
- `$ sed '$d' filename`
- 323. Replace all occurrences of a string in the second half of the file
- `$ sed 'n,$s/old_string/new_string/g' filename`
- 324. Remove all characters after the first comma
- `$ sed 's/,.//' filename`
- 325. Replace all occurrences of a string from the first line to the 10th line
- `$ sed '1,10s/old_string/new_string/g' filename`
- 326. Remove trailing spaces and tabs from all lines
- `$ sed 's/[[:space:]]$//' filename`
- 327. Remove blank lines
- `$ sed '/^$/d' filename`
- 328. Change the first occurrence of a string only on lines 10 to 20
- `$ sed '10,20s/old_string/new_string/' filename`
- 329. Replace all tabs with spaces
- `$ sed 's/\t/ /g' filename`
- 330. Replace all lowercase vowels with a number
- `$ sed 's/[aeiou]/1/g' filename`
- 331. Print all lines except for the ones containing a string
- `$ sed '/pattern/!p' filename`
- 332. Replace the first occurrence of a string on the last line of the file
- `$ sed '$s/old_string/new_string/' filename`
- 333. Find all the lines that do not contain a pattern
- `$ sed '/pattern/!p' filename`
- 334. Replace text globally in a file, but not on the last line
- `$ sed '$!s/old_string/new_string/g' filename`
- 335. Print the first line of the file
- `$ sed -n '1p' filename`
- 336. Add a line at the beginning of a file
- `$ sed -i '1i\New line at the start' filename`
- 337. Remove every second line from a file
- `$ sed 'n!d' filename`
- 338. Insert a line before every line containing a pattern
- `$ sed '/pattern/i\New line before match' filename`
- 339. Delete all lines that match a pattern
- `$ sed '/pattern/d' filename`
- 340. Replace the last occurrence of a string in the file
- `$ sed 's/old_string/new_string/g' filename | tac | sed 's/old_string/new_string/' | tac`
- 341. Print lines 5 through 10
- `$ sed -n '5,10p' filename`
- 342. Remove all lines except for the ones that match a pattern
- `$ sed '/pattern/!d' filename`
- 343. Replace the first word of every line with another word
- `$ sed 's/^\w\+/new_word/' filename`
- 344. Insert a string at the end of each line
- `$ sed 's/$/New text/' filename`
- 345. Replace every occurrence of a string globally
- `$ sed 's/old_string/new_string/g' filename`
- 346. Remove the last word from each line
- `$ sed 's/\s\+\S$//' filename`
- 347. Remove lines that contain digits
- `$ sed '/[0-9]/d' filename`
- 348. Replace text in a specific line range
- `$ sed '10,20s/old_string/new_string/g' filename`
- 349. Replace the nth word on each line
- `$ sed 's/\(\(\S\s\)\{n-1\}\)\S/\1new_word/' filename`
- 350. Print every third line
- `$ sed -n 'n~3p' filename`
- 351. Print all lines before a pattern match
- `$ sed -n '1,/pattern/p' filename`
- 352. Print all lines after a pattern match
- `$ sed -n '/pattern/,$p' filename`
- 353. Remove all spaces in a file
- `$ sed 's/ //g' filename`
- 354. Replace the first occurrence of a string only on the last line
- `$ sed '$s/old_string/new_string/' filename`
- 355. Delete lines from the beginning until a pattern match is found
- `$ sed '/pattern/,$d' filename`
- 356. Add a line at the end of a file
- `$ sed -e '$a\New last line' filename`
- 357. Replace string only in the first 5 lines
- `$ sed '1,5s/old_string/new_string/g' filename`
- 358. Replace the nth occurrence of a string in a line
- `$ sed 's/old_string/new_string/n' filename`
- 359. Replace a string but preserve the case of the original
- `$ sed 's/old_string/\L&/g' filename`
- 360. Print the first and last line of a file
- `$ sed -n '1p; $p' filename`
- 361. Replace a string on every line except for the first
- `$ sed '1!s/old_string/new_string/g' filename`
- 362. Insert a line after a specific line number
- `$ sed 'n a\New content' filename`
- 363. Replace string globally, ignoring case
- `$ sed 's/old_string/new_string/I' filename`
- 364. Print lines that start with a capital letter
- `$ sed -n '/^[A-Z]/p' filename`
- 365. Insert a line before a match and after a match
- `$ sed '/pattern/{i\Before; a\After;}' filename`
- 366. Remove trailing spaces from each line
- `$ sed 's/[ \t]$//' filename`
- 367. Replace all occurrences of a string except for the last one
- `$ sed '$!s/old_string/new_string/g' filename`
- 368. Replace the last word of each line
- `$ sed 's/\(\S\)$/new_word/' filename`
- 369. Print every even-numbered line
- `$ sed -n 'n~2p' filename`
- 370. Change the nth word of each line
- `$ sed 's/\(\w\+\) \(\w\+\) \(\w\+\)/\1 new_word \3/' filename`
- 371. Add a prefix to every line in the file
- `$ sed 's/^/Prefix /' filename`
- 372. Replace a string in lines that do not match a pattern
- `$ sed '/pattern/!s/old_string/new_string/' filename`
- 373. Remove all spaces in a line
- `$ sed 's/ //g' filename`
- 374. Print a line only if it contains more than 5 words
- `$ sed -n '/\(\w\+\s\)\{5,\}/p' filename`
- 375. Remove lines containing specific characters
- `$ sed '/[#$@]/d' filename`
- 376. Print lines between the first match and last match of a pattern
- `$ sed -n '/start_pattern/,/end_pattern/p' filename`
- 377. Insert a string after a specific word
- `$ sed 's/\(pattern\)/\1 new_string/' filename`
- 378. Remove lines from a specific pattern to the end of the file
- `$ sed '/pattern/,$d' filename`
- 379. Replace the first occurrence of a string in each line
- `$ sed 's/old_string/new_string/' filename`
- 380. Replace a word with a different one in lines containing a pattern
- `$ sed '/pattern/s/old_word/new_word/' filename`
- 381. Delete the first word from every line
- `$ sed 's/^\S\s//' filename`
- 382. Insert a line before a pattern match
- `$ sed '/pattern/i\New line before match' filename`
- 383. Print lines after the nth occurrence of a pattern
- `$ sed -n '/pattern/{n;p}' filename`
- 384. Add text after each line matching a pattern
- `$ sed '/pattern/s/$/ New text/' filename`
- 385. Print the lines before a specific pattern match
- `$ sed -n '1,/pattern/p' filename`
- 386. Replace a pattern only in the lines with more than 5 words
- `$ sed '/\(\w\+\s\)\{5,\}/s/old_string/new_string/' filename`
- 387. Replace the nth word in a specific line
- `$ sed '5s/\(\w\+\) \(\w\+\)/\1 new_word/' filename`
- 388. Remove all blank lines from a file
- `$ sed '/^$/d' filename`
- 389. Remove everything before a specific character
- `$ sed 's/^[^pattern]//' filename`
- 390. Add a suffix to every line in the file
- `$ sed 's/$/Suffix/' filename`
- 391. Print all lines from a specific match to the end of the file
- `$ sed -n '/pattern/,$p' filename`
- 392. Remove the first word and space in each line
- `$ sed 's/^\S //' filename`
- 393. Remove trailing white spaces at the end of the file
- `$ sed 's/[ \t]$//' filename`
- 394. Insert multiple lines after a pattern
- `$ sed '/pattern/a\Line 1\nLine 2' filename`
- 395. Replace only the last occurrence of a string in the file
- `$ sed 's/old_string/new_string/g' filename | tac | sed 's/old_string/new_string/' | tac`
- 396. Replace multiple spaces with a single space
- `$ sed 's/ / /g' filename`
- 397. Delete lines that are greater than 80 characters
- `$ sed '/^.\{80\}/d' filename`
- 398. Remove all digits from the file
- `$ sed 's/[0-9]//g' filename`
- 399. Print the lines after a specific string match
- `$ sed -n '/start_pattern/,$p' filename`
- 400. Remove lines matching a pattern
- `$ sed '/pattern/d' filename`
- 401. Add a line at the end of a specific line number
- `$ sed '5a\New line' filename`
- 402. Replace the second word in each line
- `$ sed 's/\(\S\+\) \(\S\+\)/\1 new_word/' filename`
- 403. Insert a new line after a pattern match
- `$ sed '/pattern/a\New line after match' filename`
- 404. Remove all lines containing only whitespace
- `$ sed '/^[[:space:]]$/d' filename`
- 405. Print only the last line of the file
- `$ sed -n '$p' filename`
- 406. Replace the first occurrence of a string on a specific line
- `$ sed '5s/old_string/new_string/' filename`
- 407. Remove everything before the first comma
- `$ sed 's/^[^,],//' filename`
- 408. Replace only the nth occurrence in a line
- `$ sed 's/old_string/new_string/n' filename`
- 409. Print all lines that contain a pattern
- `$ sed -n '/pattern/p' filename`
- 410. Replace the first 10 occurrences of a string
- `$ sed 's/old_string/new_string/10' filename`
- 411. Remove lines between two patterns
- `$ sed '/start_pattern/,/end_pattern/d' filename`
- 412. Replace spaces with underscores
- `$ sed 's/ /_/g' filename`
- 413. Remove leading spaces from each line
- `$ sed 's/^[[:space:]]//' filename`
- 414. Print all lines except the first 10
- `$ sed '1,10d' filename`
- 415. Delete all lines after the 10th line
- `$ sed '10,$d' filename`
- 416. Insert a line at the beginning of the file
- `$ sed -i '1i\New first line' filename`
- 417. Replace a string only in a specific line range
- `$ sed '5,10s/old_string/new_string/g' filename`
- 418. Replace a string only in the first match on each line
- `$ sed 's/old_string/new_string/' filename`
- 419. Remove the last character of every line
- `$ sed 's/.$//' filename`
- 420. Delete a specific word from all lines
- `$ sed 's/word//g' filename`
- 421. Replace commas with spaces
- `$ sed 's/,/ /g' filename`
- 422. Replace the nth character of every line
- `$ sed 's/^\(.\{n\}\)./\1new_char/' filename`
- 423. Delete lines that match a pattern, but keep their content
- `$ sed '/pattern/{N; s/\n//;}' filename`
- 424. Remove all lines starting with a specific character
- `$ sed '/^#/d' filename`
- 425. Insert a line at the end of the file
- `$ sed -e '$a\New last line' filename`
- 426. Replace the last occurrence of a word in the file
- `$ sed 's/old_word/new_word/g' filename | tac | sed 's/old_word/new_word/' | tac`
- 427. Print every other line starting from the first
- `$ sed -n '1~2p' filename`
- 428. Replace all uppercase letters with lowercase
- `$ sed 's/[A-Z]/\L&/g' filename`
- 429. Print every line that matches a pattern, and the next line after
- `$ sed -n '/pattern/{n;p}' filename`
- 430. Remove empty lines
- `$ sed '/^$/d' filename`
- 431. Replace a string on a specific line
- `$ sed '5s/old_string/new_string/' filename`
- 432. Replace a string only if it's on the first word of a line
- `$ sed 's/^old_string/new_string/' filename`
- 433. Replace every instance of a string, ignoring case
- `$ sed 's/old_string/new_string/I' filename`
- 434. Print lines between two patterns (inclusive)
- `$ sed -n '/start_pattern/,/end_pattern/p' filename`
- 435. Remove everything after a comma in each line
- `$ sed 's/,.//' filename`
- 436. Replace a word with its uppercase version
- `$ sed 's/word/\U&/g' filename`
- 437. Replace all dots with commas
- `$ sed 's/\./,/g' filename`
- 438. Remove all lines after a pattern match
- `$ sed '/pattern/,$d' filename`
- 439. Insert a line before every line with a number
- `$ sed '/[0-9]/i\New line before numbers' filename`
- 440. Replace all occurrences of a string with its reverse
- `$ sed 's/old_string/'$(echo old_string | rev)'/g' filename`
- 441. Replace only the first occurrence of a pattern in a line
- `$ sed 's/pattern/new_string/' filename`
- 442. Remove trailing tabs from each line
- `$ sed 's/\t$//' filename`
- 443. Print every line that doesn’t contain a specific pattern
- `$ sed -n '/pattern/!p' filename`
- 444. Print the last 10 lines
- `$ sed -n '10,$p' filename`
- 445. Replace the second occurrence of a string in a line
- `$ sed 's/old_string/new_string/2' filename`
- 446. Add a prefix to every line
- `$ sed 's/^/Prefix /' filename`
- 447. Print lines starting with a specific character
- `$ sed -n '/^#/p' filename`
- 448. Remove specific characters from a file
- `$ sed 's/[[:punct:]]//g' filename`
- 449. Replace multiple spaces with a single space
- `$ sed 's/ / /g' filename`
- 450. Replace all spaces with tabs
- `$ sed 's/ /\t/g' filename`
- 451. Change the case of each word
- `$ sed 's/\w\+/\u&/g' filename`
- 452. Remove lines that contain only digits
- `$ sed '/^[0-9]$/d' filename`
- 453. Add a string to the beginning of each line
- `$ sed 's/^/New string /' filename`
- 454. Remove all words starting with a specific letter
- `$ sed 's/\b[aA]\w\b//g' filename`
- 455. Replace a string globally only if it matches the whole line
- `$ sed '/^old_string$/s/old_string/new_string/' filename`
- 456. Insert text before a pattern
- `$ sed '/pattern/i\New text before pattern' filename`
- 457. Delete lines that are longer than 80 characters
- `$ sed '/^.\{80\}/d' filename`
- 458. Replace the nth word of every line with a string
- `$ sed 's/\(\w\+\) \(\w\+\) \(\w\+\)/\1 new_word \3/' filename`
- 459. Delete lines that don’t contain a pattern
- `$ sed '/pattern/!d' filename`
- 460. Remove characters from a specific position to the end of each line
- `$ sed 's/^\(.\{n\}\)./\1/' filename`
- 461. Replace first occurrence of a string in the last 5 lines
- `$ sed '$-5,$s/old_string/new_string/' filename`
- 462. Delete lines from the first occurrence of a pattern to the last line
- `$ sed '/pattern/,$d' filename`
- 463. Print lines that do not match a pattern
- `$ sed -n '/pattern/!p' filename`
- 464. Replace all hyphens with spaces
- `$ sed 's/-/ /g' filename`
- 465. Print only lines starting with a specific word
- `$ sed -n '/^start_word/p' filename`
- 466. Remove blank lines with tabs or spaces
- `$ sed '/^[[:space:]]$/d' filename`
- 467. Print a range of lines starting from a specific line
- `$ sed -n '5,15p' filename`
- 468. Replace the nth occurrence of a pattern in the whole file
- `$ sed 's/old_string/new_string/n' filename`
- 469. Remove lines that contain a pattern and their surrounding lines
- `$ sed '/pattern/{N; d;}' filename`
- 470. Replace a string on lines containing another string
- `$ sed '/pattern/s/old_string/new_string/' filename`
- 471. Replace the first word in a line with another word
- `$ sed 's/^\w\+/new_word/' filename`
- 472. Remove lines with the pattern at the start and end of the line
- `$ sed '/^start_pattern$/d' filename`
- 473. Remove the last word of the last line
- `$ sed '$s/\s\+\S$//' filename`
- 474. Replace tabs with a specific number of spaces
- `$ sed 's/\t/ /g' filename`
- 475. Add a suffix to the end of every line
- `$ sed 's/$/ New suffix/' filename`
- 476. Replace all numbers with a word
- `$ sed 's/[0-9]\+/word/g' filename`
- 477. Replace the nth occurrence of a string on specific lines
- `$ sed '5,10s/old_string/new_string/n' filename`
- 478. Print all lines except the ones starting with a specific letter
- `$ sed '/^A/!p' filename`
- 479. Delete all lines that contain only alphabets
- `$ sed '/^[A-Za-z]$/d' filename`
- 480. Insert a line after every pattern match
- `$ sed '/pattern/a\New line after match' filename`
- 481. Replace a string globally only for even-numbered lines
- `$ sed 'n~2s/old_string/new_string/g' filename`
- 482. Remove all non-alphabetic characters from the file
- `$ sed 's/[^A-Za-z]//g' filename`
- 483. Print every line except the ones with specific characters
- `$ sed '/[#$@]/!p' filename`
- 484. Remove all commas and replace them with a space
- `$ sed 's/,/ /g' filename`
- 485. Delete all lines with the string "delete"
- `$ sed '/delete/d' filename`
- 486. Remove extra spaces and convert them to a single space
- `$ sed 's/ / /g' filename`
- 487. Replace every tab with four spaces
- `$ sed 's/\t/ /g' filename`
- 488. Delete lines that match a pattern but add a comment
- `$ sed '/pattern/{s/.$/#Commented line/;}' filename`
- 489. Replace all words with "word"
- `$ sed 's/\w\+/word/g' filename`
- 490. Change case of a word globally
- `$ sed 's/old_word/\U&/g' filename`
- 491. Add a line before every pattern match
- `$ sed '/pattern/i\New line before match' filename`
- 492. Delete the first word from each line
- `$ sed 's/^\S\s//' filename`
- 493. Replace every occurrence of a string in a specific column
- `$ sed 's/\(\S\s\)\{n\}old_string/new_string/' filename`
- 494. Delete all empty lines from a file
- `$ sed '/^$/d' filename`
- 495. Remove all instances of a string from a file
- `$ sed 's/old_string//g' filename`
- 496. Print only lines matching a specific word and add a suffix
- `$ sed '/pattern/s/$/ suffix/' filename`
- 497. Delete lines that are over a certain length
- `$ sed '/^.\{80\}/d' filename`
- 498. Replace a string and add it to the end of the line
- `$ sed 's/old_string/& appended_string/' filename`
- 499. Replace all digits with a placeholder
- `$ sed 's/[0-9]/X/g' filename`
- 500. Remove lines that start with a specific character #
- `$ sed '/^#/'d filename`
- 501. Print lines start with specific character #
- `$ sed -n '/^#/p' filename`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement