Icon Fame Journal.

Juicy entertainment chatter with tabloid flavor.

updates

How to use the sed command on linux

By Rachel Davis

Here are all the possible meanings and translations of the word SED.

Wiktionary (5.00 / 1 vote) Rate this definition:

A noninteractive text editor (originally developed in Unix), intended for making systematic edits in an automatic or batch-oriented way.

Etymology: From stream editor.

To edit a file or stream of text using sed.

Can you sed out those trailing spaces, please?

Etymology: From stream editor.

Freebase (0.00 / 0 votes) Rate this definition:

sed is a Unix utility that parses and transforms text. sed reads text input, line by line, either from a file or a stream, into an internal buffer called the pattern space. Each line read starts a cycle. To the pattern space, sed applies one or more operations which have been specified via a sed script. sed implements a programming language with about 25 commands that specify the operations on the text. When the sed script ends, sed outputs the pattern space, and reads the next line into the pattern space, starting a new cycle. The sed script can either be specified on the command line or read from a separate file. Commands in the sed script may also take an optional address. The address determines when the command is run, for example ‘2d’ would only run the d command on the second input line. Some sed operations can use regular expressions to add flexibility. And a separate special buffer, the hold space, may be used by a few sed commands to hold and accumulate text between cycles. sed was developed from 1973 to 1974 as a Unix utility by Lee E. McMahon of Bell Labs, and is available today for most operating systems.

Chambers 20th Century Dictionary (0.00 / 0 votes) Rate this definition:

sed, n. a line fastening a fish-hook: a snood.

The New Hacker’s Dictionary (5.00 / 1 vote) Rate this definition:

[TMRC, from ‘Light-Emitting Diode’] Smoke-emitting diode. A friode that lost the war. See also LER. [Not to be confused with sed(1), the Unix stream editor. —ESR]

  1. w command : Show who is logged on and what they are doing on Linux
  2. who command : Display information about Linux users who are currently logged in
  3. whoami command : Find out who you are currently logged in as on Linux
  4. id command : See user and group information for the specified USER account on Linux

All user names are stored in /etc/passwd file and can be displayed with help of cat command or grep command/egrep command:
cat /etc/passwd
grep ‘^userNameHere’ /etc/passwd
grep ‘^tom’ /etc/passwd
Let us see all examples and usage in details.

How to show current logged in users in Linux

Open the terminal window and type:
w
How to use the sed command on linux
The w command shows information about the Linux users currently on the server, and their running processes. The first line displays, in this order:

  • The current time ( 22:11:17 )
  • How long the Linux server has been running (18 days)
  • How many users are currently logged on Linux (2 users)
  • The system load averages for the past 1, 5, and 15 minutes (1.01, 1.04, 1.05)

The following info displayed for each current logged in user:

  • sweta – Login name
  • pts/10 – The tty name
  • minitx – The remote host/desktop/laptop name
  • 22:11 – Login time
  • 5.00s – Idle time
  • 0.04s – JCPU (it the time used by all processes attached to the tty. It does not include past background jobs, but does include currently running background jobs.)
  • 0.02s – PCPU (it is the time used by the current process, named in the “what” field.)
  • vim replicant.py – The command line of their current process

Find out who you are currently logged in as on Linux

Execute the following command:
whoami
Another option is to just type the following id command:
id

How to use the sed command on linux

Use the whoami and id commands to find out who you are currently logged in as on Linux based system

Linux show who is logged on

Again run the who command:
who
OR
who -a
How to use the sed command on linux

Conclusion

This page showed how to find out current logged in users in Linux and what they are doing from the terminal. For more info see man pages using the man command:
man w
man who
man whoami

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far. add one

How to use the sed command on linux

sed stands for stream editor and is a commonly-used command in Linux/Unix. The name comes from a portmanteau of those two words. It’s not a text editor, though it does modify text. Instead, sed receives text input as a “stream” and edits the stream according to your instructions.

By and large, people use sed as a command line version of find and replace. The main command in the program is used to substitute one character string for another. This makes it useful for running find and replace functions on a batch of files, but it is not as easy to use as something from Notepad or Word. You’ll find that the syntax takes a little getting used to, but once you have that down, you’ll see how incredibly powerful this compact editor can be.

Using sed for substitution

To use sed, you’ll need to pop open a Terminal window. The command is invoked with sed , but typing that alone won’t do anything. You need to provide one of the commands, as well as some parameters.

The most common parameter is substitute, or s . A simple sed substitution command looks something like the following:

That command will order sed to scan incoming text and replace every instance of the string “cat” with “dog.” To demonstrate, we’ll use the echo command to give sed something to work with.

How to use the sed command on linux

There are three main parts to the sed command. First, there’s sed which starts the program. Then there’s the s which starts the substitution command. The forward slash / is a separator. The string after that will be the string sed looks for. The string after the second slash will be the replacement characters that sed writes to the stream. The final slash closes the command. All together, it’s ‘s/text-to-find/text-to-replace/’ .

Note the single quotes in that example. The single quotes allow you to use meta characters in your command. While quotes aren’t essential for most of our examples, you should develop the habit of using quotes for all commands.

What about partial strings? The above command will work on any appearance of the string “cat,” even if it’s part of another word. For example, “catapult” will become “dogapult” as “catamaran” will become “dogamaran.”

How to use the sed command on linux

Global replacement

In our first example you might have noticed only the first instance of the matching string was replaced. Like many GNU utilities, sed is line-based. As a result, it only operates on the first match within a line. If you have multiple matches for your candidate string in the line, only the first one will be changed. To make sed work on every occurrence of the target string, use the g flag at the end of the command:

This would operate like the following example:

How to use the sed command on linux

Without the global flag, only the first instance of “cat” in each line would be replaced.

Setting input and output files

By default, sed will operate on standard input. However, that’s not too useful. To run sed on a file, specify the filename at the end of the command, like so:

How to use the sed command on linux

This will run sed on “oldfile.txt” and save the command’s output to “newfile.txt.” If “newfile.txt” does not exist, it will be created by the command. If you don’t specify an output file, sed will echo the file’s new text into standard input.

If you want to overwrite the contents of a file, you’ll need to use the -i flag. This flag makes the edits “in place.”

This command overwrites the contents of the target file immediately, which is slightly dangerous. To stay on the safe side, you can create a backup in-situ.

To create a backup of the file, put an extension after the -i . It doesn’t need to be a functional extension: “.bu” or “.bak” work fine. This will create a backup file with an extension.

This creates “oldfile.txt.bak,” which contains the unedited text data from the original version of “oldfile.txt.”

Matching regular expressions

The real power of sed comes from matching regular expressions. This allows you to set patterns for matching, rather than literal strings. For example, we could use something like the string below to repeat any set of one or more numbers:

How to use the sed command on linux

Further Learning

sed is an extremely powerful utility with a depth of power similar to a scripting language. To learn more about sed’s full capabilities, check out Bruce Barnett’s comprehensive guide to sed.

Alexander Fox is a tech and science writer based in Philadelphia, PA with one cat, three Macs and more USB cables than he could ever use.