Sed is a non-interactive text editor. It comes from English [S]tream [ED]itor, ie text flow editor. GNU Sed is currently used by most Linux distributions and has the most new features: GNU/Linux.
It’s case sensitive
-i
change the file-e
print to screen without changing file-n
suppress, show only command outputs
replaces one piece of text with another!
inverts the logic of the command;
command separator|
string separatord
at the end deletep
at the end printsg
at the end (as d and p is used) changes all occurrencesq
quit sed, does not continue command[[: alnum:]]
Alphabetic and Numeric [a-z A-Z 0-9][[: alpha:]]
Alphabetic [a-z A-Z][[: blank:]]
Blank character, space, or tab [\ t][[: cntrl:]]
Control characters [\ x00- \ x1F \ x7F][[: digit:]]
Numbers [0-9][[: graph:]]
Any visible character (i.e. except blank) [\ x20- \ x7E][[: lower:]]
Lowercase letters [a-z][[: upper:]]
Capital letters [A-Z][[: print:]]
Visible characters (ie except control characters) [\ x20- \ x7E][[: punct:]]
Score [-! ”# $% & ‘() * +,. /:;? @ [] _` {} ~].[[: space:]]
White space [\ t \ r \ n \ v \ f][[: xdigit:]]
Hexadecimal Number [0-9 a-f A-F]
1 - Swap all words in one file for another
2 - Prints only the ninth line
3 - Prints from the sixth line to the ninth line
4 - Delete all lines containing the word string in the file.
5 - Put one word at the beginning of each line.
6 - Put a word at the end of each line.
7 - Prints only lines that START with the string ‘http’
8 - Deletes only lines that START with the string ‘http’
9 - Exchange ALL occurrences of the word “marcos”, “eric”, “camila” with the word “penguin”
10 - Exchange everything between the words “Marcos” and “Eric” for the word “they”, for example, the text is:
“On Saturday Marcos went out to bike with Eric, but they didn’t stay up late.” And it will look like this: “On Saturday they didn’t stay up late.”
11 - Delete blank line and change file
12 - Replaces “foo” with “bar” only lines containing “plop”
13 - Replaces “foo” with “bar” except lines containing “plop”
14 - Insert Line 2 with line 7 the “#” at the beginning of each line
15 - Inserts the word ‘NEW’ at the beginning of each line from line 21 to line 28
16 - Swap everything between the tags “<” and “>” for the word “CODE”:
17 - Prints only first occurrence of line with given string
18 - Include text at end of line 9
19 - Put all lines into one
20 - Replaces the word “BEAUTY” with “YES” only between certain lines.
21 - Deletes what is between the word “spoke” and “second” (delimiters)
22 - Removes HTML commands
23 - Deletes the 1st character of the sentence.
24 - Deletes the 4th character of the sentence.
25 - Deletes the first 4 characters
26 - Deletes at least 4 characters
27 - Deletes 2 to 4 characters
28 - Range Examples
29 - Transforms text (URL) into HTML link tags.
It was: http://www.com
It is: <a href="http://www.com">http://www.com</a>
30 - Regular Expressions with SED (sed regex)
This sed reads data from file.txt and erases (command d) from the first line to the line containing 3 numbers in a row, throwing the result on the screen. If you want to save the result, redirect it to another file, not the file.txt itself.
Delete numbers
Prints only lines containing SCORE
Print only lines beginning with Numbers
We have a file with the phone numbers like this:
Running some of these command modes in SED:
Replaces 2 characters “..” with “&” which is the output of the request. Performs another sed to replace 8 characters again with “&” Note: You must always escape the parentheses “(“ and “)”
Same as above, just put the “{8}” to mark 8 characters “.” Also, ALWAYS need to escape the “{“ and “}” keys
Instead of throwing the exit, I separated the command with a semicolon “;” and launched another sed “s”
This mode is to understand the following
The first command in parentheses “(..)” Then separated by slash
I threw or command in parentheses “\ (.{4 } )” The first command output goes to bar 1 “\1” And the second command for slash 2 “\2” It could also have bar 3, n,…
Stays like this:
Thanks for read.