If a picture is worth 1000 words, then said one liners are definitely worth 1000 lines of code. One of the most feared commands in the Linux CLI is, you guessed it said it's been feared by programmers and sis admins everywhere because of its cryptic usage, but it can serve as a very powerful tool for quickly editing large amounts of data. I have created five files to help demonstrate the power of this awesome tool. The first one is a simple file containing the humble line of text. Orange is the New Black. Let's start by creating a simple stretch command to replace the word black with white.
The first argument of said is the Replace command. It's divided into three parts by three dashes. The first part is as for substitutes The second part is the word to be replaced black in our case. And the third part is the replacement word white. The second argument is the input in our case of file. Okay, now the result will be printed on the screen, and you can see the word Black has been replaced by white.
Our second example, contains yet another line of text, this time with the word black in both upper and lowercase. If we run the same command using this new file, we will see that it replaces only the word that matches the case. If we want to do a case insensitive replace, we will add two more characters to the end of our site Command G and big g means global replace. It's used for replacing all the occurrences in the file. Without this it will only replace The first argument b e means case insensitive search. Here it is.
And as you can see both words have been replaced. If we want to save the results in our file, instead of printing to the screen, we use the E argument which stands for inline replace. In some scenarios, we might also want to save our initial files, just in case we have an error in the set command. To do this, we specify a suffix after E, which will create a backup file. In our case, we will use the dot bak suffix. If we check the content of the file, we can see that the initial file contains the updated text and the backup file contains the original text.
Now, let's To look at a more practical example, let's say we have a shell script that contains multiple variables. And we want to surround our variables with curly brackets. In order to do this we will write as for substitute g for global, meaning replace all occurrences found. dollar slash dollar actually match or string starting with $1 sign. Here dollar needs to be escaped so that it's not confused with the start of Euro anchor. So it's slash dollar.
We will enclose the string following the dollar in parenthesis so that we can reference it in the replaced part of our command. square brackets for specifying a range of characters. A to Z to match all uppercase characters, zero to nine to match all numbers. underline to match underline search plus, which means any character in the square brackets must appear one or multiple types. In the Replace part we use, the dollar sign escaped the curly brackets we want to add, and the string that was previously matched in the parentheses. Seems like in our example, we forgot the opening parenthesis.
Other common scenarios are replacing content in XML or HTML files. Here we have a basic HTML file with a strong text inside. Now we know that the strong text has more semantic value for search engine optimizations. So maybe we want to make our strong tax bureau simple Be bold and manually decide the strong words in the page. For this we say substitute the actual text we are searching for. This will be used again for selecting a piece of text that will be added back.
This means any character found any number of times we want to select everything between the starting strong and the end strong. closing of the tag this we want to keep intact and just add BB and the text that you previously found in the parentheses. Okay. It seems as a foreign language at first but with time, you will understand still need we need to fix a small typo. He is here and as you can see the text was updated correctly the red class still applies to the new tag The old tax is still contained between our tags, which is exactly what we wanted. Besides replacing set can also be used for deleting lines of text.
Our five dot txt file contains all the words from the lorem ipsum text. If we wanted to delete the third line of text, we would issue the command said line ii 3d five point. txt Oh my God, I'm not going to repeat them again. Now we will load the file in vim, and we see that the word is no longer there. If for example, we want to delete the first 10 lines of the file, we simply run this command. Okay.
Now we see that the lines are no longer there. And for our last example, if you Scroll down, we can see multiple empty lines of text. These can be deleted with the following, comma which stands for beginning of line and core end of line, anchor and delete. Reload the file and you'll see that the lines are no longer there. Now, as you can imagine, these have only been some basic examples. The power of set is much greater than this, and there are way more possibilities of using it.
Then what we have seen today, we recommend that you gain a good understanding of the features presented here today, as these are the features you will probably use the most. It's not as complicated as it may seem at first, and it really comes in handy in lots of scenarios.