How to filter text with the sed command. This is useful for various shell tricks.

Posted: July 1, 2013. At: 10:02 PM. This was 4 years ago. Post ID: 5905
Page permalink: http://securitronlinux.com/bejiitaswrath/how-to-filter-text-with-the-sed-command-this-is-useful-for-various-shell-tricks/

Using wildcards on the Linux command line

This is a standard listing of files with wildcards in the bash shell.

homer@deep-thought ~/Documents $ ls *.wad
basenew.wad  cc4-tex.wad  city-heat.wad    dark.wad   doom.wad   plutonia.wad  scythe2.wad  SKYTEST.wad
brick.wad    cchest4.wad  consoleCopy.wad  doom2.wad  hexen.wad  RIII.wad      scythex.wad  SODfinal.wad

And this is how to use the sed command to filter the output to change it.

homer@deep-thought ~/Documents $ ls *.wad | sed s/wad/fad/gi;
basenew.fad
brick.fad
cc4-tex.fad
cchest4.fad
city-heat.fad
consoleCopy.fad
dark.fad
doom2.fad
doom.fad
hexen.fad
plutonia.fad
RIII.fad
scythe2.fad
scythex.fad
SKYTEST.fad
SODfinal.fad

Here is another example of modifying the output to show something apart from what was originally there. Changing the month to December.

homer@deep-thought ~/Documents $ ls -hula *.wad | sed s/Jun/Dec/gi;
-rw-r--r-- 1 homer homer 548K Dec 15 17:53 basenew.wad
-rw------- 1 homer homer 1.1M Dec  1 11:25 brick.wad
-rw-r--r-- 1 homer homer  13M Aug 17  2012 cc4-tex.wad
-rw-r--r-- 1 homer homer  70M Dec 15 17:53 cchest4.wad
-rw------- 1 homer homer 3.3M Dec  1 11:25 city-heat.wad
-rw------- 1 homer homer 2.4M Dec  1 11:25 consoleCopy.wad
-rw------- 1 homer homer 1.8M Dec  1 11:25 dark.wad
-rw------- 1 homer homer  14M Dec 29 22:05 doom2.wad
-rw------- 1 homer homer  12M Dec 29 22:05 doom.wad
-rwxrwxrwx 1 homer homer  20M Dec 29 22:05 hexen.wad
-rw------- 1 homer homer  17M Dec 29 22:05 plutonia.wad
-rw-r--r-- 1 homer homer  19M Dec  1 11:25 RIII.wad
-rw------- 1 homer homer  30M Dec  1 11:25 scythe2.wad
-rw------- 1 homer homer  22M Dec  1 11:25 scythex.wad
-rw-rw-r-- 1 homer homer 3.4K Dec  1 11:25 SKYTEST.wad
-rw-r--r-- 1 homer homer  28M Dec  1 11:25 SODfinal.wad

This is something I am very proud of. This is how to change the M for Megabytes in the ls -hula output into the word Megabytes. So cool.

homer@deep-thought ~/Documents $ ls -hula *.wad | sed s/M/\ Megabytes/;
-rw-r--r-- 1 homer homer 548K Jun 15 17:53 basenew.wad
-rw------- 1 homer homer 1.1 Megabytes Jun  1 11:25 brick.wad
-rw-r--r-- 1 homer homer  13 Megabytes Aug 17  2012 cc4-tex.wad
-rw-r--r-- 1 homer homer  70 Megabytes Jun 15 17:53 cchest4.wad
-rw------- 1 homer homer 3.3 Megabytes Jun  1 11:25 city-heat.wad
-rw------- 1 homer homer 2.4 Megabytes Jun  1 11:25 consoleCopy.wad
-rw------- 1 homer homer 1.8 Megabytes Jun  1 11:25 dark.wad
-rw------- 1 homer homer  14 Megabytes Jun 29 22:05 doom2.wad
-rw------- 1 homer homer  12 Megabytes Jun 29 22:05 doom.wad
-rwxrwxrwx 1 homer homer  20 Megabytes Jun 29 22:05 hexen.wad
-rw------- 1 homer homer  17 Megabytes Jun 29 22:05 plutonia.wad
-rw-r--r-- 1 homer homer  19 Megabytes Jun  1 11:25 RIII.wad
-rw------- 1 homer homer  30 Megabytes Jun  1 11:25 scythe2.wad
-rw------- 1 homer homer  22 Megabytes Jun  1 11:25 scythex.wad
-rw-rw-r-- 1 homer homer 3.4K Jun  1 11:25 SKYTEST.wad
-rw-r--r-- 1 homer homer  28 Megabytes Jun  1 11:25 SODfinal.wad

And an example using Kilobytes.

homer@deep-thought ~/Documents $ ls -hula *.wad | grep basenew | sed s/K/\ Kilobytes/;
-rw-r--r-- 1 homer homer 548 Kilobytes Jun 15 17:53 basenew.wad

No comments have been made. Use this form to start the conversation :)

Leave a Reply