'sed' command in Linux

in #sed2 years ago

Yesterday, I heard about and tested the 'sed' command for the first time in my life. I have been working with quite a lot of text editing commands recently, and also command that deal with how you find and get information in files, such as sort, awk, cut, wc, find, and quite a lot of others. It is interesting, but it is one thing to understand the command, another thing to remember it in the time of need. As a result, it is useful to repeat the commands after a few days, just to check that you actually remember how they can be used and what they can be used for.

Right now, I just realized that I have forgotten really how to use the find command, so I just did a bit of testing to check out how it is working and repeat some of my earlier lesson.

What do I mean?

It isn't enough to just type the command find ipguide and then suddenly everything will show up. You will have to to tell the command where to look and also what you are looking for. It is also possible to tell what the system should do with the files it find. For example:

find /home/ -name ipguide.txt -delete

If you run that command, the command will search the /home/ category for the file named ipguide.txt and if it finds anything, it will be deleted. If you would remove the -delete command in the end, it would just show you the location of the file.

But, let us return to the 'sed' command. What is it about? it is useful for finding content, replacing content, and much more. Let me show you an example.

sed 's/ipguide/stupid/g' ipguide.txt

If I run that command, it will automatically substitute the word ipguide with stupid globally (on every occasion). That is useful, isn't it. The result will be shown immediately. It is, however, important to know that the changes will only happen on screen, and not in the file itself. If you show the content of the file afterward, it will still be like before you ran the command.

If you want the changes to be shown in the file, there are two options.

sed -i 's/ipguide/stupid/g' ipguide.txt (the changes will be made directly in the file).
sed 's/ipguide/stupid/g' ipguide.txt > newfile.txt (the changes will be made in the new file newfile.txt).

These are both useful options and worth knowing about.

Do you have a long list of names that you want to make look nicer? Would it be cool to swap every single space with a tab. Run the following command.

sed 's/ /\t/g' ipguide.txt

There you go, yet another example of the sed command in Linux. Wasn't that cool as well?

Just a little bonus. I had some trouble finding a button with my keyboard settings, due to a wrong keyboard layout in use. How can I change the keyboard layout in the command line of CentOS and Ubuntu?

localectl status

This command will show you the current settings.

localectl set-keymap hu

This will set your permanent keyboard settings to Hungarian. You can swap to other languages available. You can find available languages with the following command.

localectl list-keymap
or with
localectl list-xii-keymap-layouts

Now you have a lot to choose from.

Coin Marketplace

STEEM 0.15
TRX 0.12
JST 0.025
BTC 55258.26
ETH 2459.89
USDT 1.00
SBD 2.19