Sed: Learning Shell Scripting Using the Sed Terminal Command

Sed: Learning Shell Scripting Using the Sed Terminal Command

Sed: Learning Shell Scripting Using the Sed Terminal Command

The command "sed" in Free Operating Systems of the GNU / Linux type it is a very versatile command, since in turn It is considered a text editor oriented to "flow" instead of "interactive" as the typical ones, as it accepts a file or standard input as input, then processes each line and the result is sent to standard output. That is to say, SED (Stream EDitor) allows us to modify the content of the different lines of a file based on a series of commands or a command file.

Generally, the command «sed» is used as a filter, since it reads each line of your data entry and then performs the requested actions that are usually read, extract, modify and show or save. Although its power is such that additionally it allows us in a comfortable way, delete lines, records or replace character strings within the lines of a file.

Introduction to the "sed" command

Already in other previous publications in DesdeLinux we have talked and shown examples about the use of said command, such as: How to delete specific lines from a file ... y With Terminal: Using Regular Expressions II: Replacements, so this time we will directly see a little more advanced and just as practical examples using it.

Basically the command «sed» has the following syntax:

sed comandos_sed archivo

Where "Command_sed" corresponds to one or more specifications about what to do with the lines that are read from "archive".

A more extensive use leaves us with the following syntax:

sed [-ns] '[direccion] instruccion argumentos'

Where:

SED Command Usage Format

Practical examples

List Files / Folders in a path

Useful command line that can be used to list the content of a specified X path and then detect / count / filter a searched item and indicate a subsequent action to be performed.

ls -l | awk '{ print $NF }' | sed 1d

Example:

Example1: SED command - Shell Scripting

This other variant using the "sed" command is very useful:

find /opt/MilagrOS/file-apps/fondos_pantalla/ -type f | sed 's/^\.\///g' | xargs -n 1 basename

Example 4: SED command for Shell Scripting

Create a number sequence

Show on screen or save in a file a numerical sequence (or any other input) created but in horizontal (linear) and not vertical form that can later be read for other processes.

seq 100|tr '\n' ' '|sed 's, $,\n,g'

Example 2: SED command for Shell Scripting

The same example can be done by combining the command «thirst" with "Printf" as shown below:

printf '%s ' {1..100}|sed 's/ $/\n/'

Example 3: SED command for Shell Scripting

Delete files with exclusions

Sifting through a File System path looking for patterns of files to exclude and then deleting the rest of the content. Very useful for precise and careful cleaning.

find /opt/MilagrOS/file-apps/fondos_pantalla/ -type f | sed -e '/.*\.txt$/d' -e '/.*\.exe$/d' | xargs rm

Example 5: SED command for Shell_Scripting

Create a virtual MAC address

This step can be useful for computer security or protection tasks when browsing dangerous sites or carrying out hacking tasks.

MAC=`(date; cat /proc/interrupts) | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'` ; echo $MAC

The same effect can be achieved by using the "sed" command in conjunction with other commands, as shown below:

openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'
od -An -N10 -x /dev/random | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'
od /dev/urandom -w6 -tx1 -An|sed -e 's/ //' -e 's/ /:/g'|head -n 1

Example:

Example 7: SED command for Shell Scripting

Temporarily change the Terminal Prompt

This can help us to temporarily insert parameters in our prompt that can be useful to us while we carry out a special activity. In this example we will only change the way the prompt is displayed.

export PS1="C:\$( pwd | sed 's:/:\\\\\\:g' )\\> "

Example 8. SED command for Shell Scripting

Manage the output of the "ps" Command

Very practical command order to allow the formatting of the output of the «ps» command, that is, what is obtained from the Terminal Process Manager.

sudo ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed "/^ 0.0 /d"

Example:

Example 9: SED command for Shell Scripting

This can be applied for processes according to the percentage of memory consumed:

sudo ps -e -o pmem,nice,state,cputime,args --sort pmem | sed "/^ 0.0 /d"

These examples of command lines using the command "sed" I hope you find it very useful. And they can be used to be used directly from the console or indirectly by inserting them in a script (automated task).

In the next post on Shell Scripting we will explore other commands.

If you want to learn a little more about the command "sed" visit this link "Tutorial and Brief Guide for Unix and Linux"Or"Basic use»And if you want to learn more about Shell Scripting in our own Blog, you can take a look at our other publications on the subject, by clicking here: Scripting DesdeLinux.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.