A Stream EDitor is used to perform basic transformations on text read from a file or a pipe. The result is sent
to standard output. The syntax for the sed command has no output file specification, but results can be saved
to a file using output redirection. The editor does not modify the original input.
Command Result
a\ Append text below current line
c\ Change text in the current line with new text
d Delete text
i\ Insert text above the current line
p Print text
r Read a file
s Search and replace text
w Write to a file
This is our example text file: (FILENAME : fuckthecode)
$ cat -n fuckthecode
1 First line fcukthecode
2 second line fcuk the code
3 third line fuckkk thee codee
4 fourth line fuk the coed
5 fifth line and fuckin yeah
Now, we want to take out the lines 2 to 4. Specify this range to address, together with the d command:
sed ‘2,4d’ filename (lines 2 to 4) (d command is for deleting text)
$ sed '2,4d' fuckthecode
First line fcukthecode
fifth line and fuckin yeah
To delete the file starting from a certain line until the end of the file, use a command similar to this:
$ sed '2,$d' fuckthecode
First line fcukthecode
Now, we want to print the lines 2 to 4. Specify this range to address, together with the p command:
$ sed -n '2,4p' fuckthecode
second line fcuk the code
third line fuckkk thee codee
fourth line fuk the coed
To print the file starting from a certain line until the end of the file, use a command $ to this:
$ sed -n '2,$p' fuckthecode
second line fcuk the code
third line fuckkk thee codee
fourth line fuk the coed
fifth line and fuckin yeah
More Q
- Find the majority element appearing more than n/2 times
- Return an array of the squares of each number.
- Highest factor in addition.
- Find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).
- Every element in array appears twice.Find that single one.
- Find the duplicate letters in string. Same to do in dictionary way.
- Find minimum number of edits (operations) required to convert ‘str1’ into ‘str2’.
- Return the maximum number you can get by changing at most one digit.
- Sed Commands – Ranges of lines.
- Find the kth smallest element in the given 2D array.
- Find all elements that appear more than [n/3] times.
- Write a function to return true if s2 contains the permutation of s1.
- Sort the array into a wave like array.
- Re-order array, such that
nums[0] < nums[1] > nums[2] < nums[3]...
. - Given the coordinates, return true if the four points construct a square.
- Minimum Distance Between Words of a String.
- Return the shortest distance between these two words.
- Write a program to find the n-th ugly number.
- Check if array could become Non – Decreasing array.
- Find the minimum area of a rectangle that can be formed from given points.