How to use the sed command to filter text files in Linux and other useful shell tricks for the Linux command line.

Posted: October 11, 2012. At: 11:26 PM. This was 5 years ago. Post ID: 4705

Using sed to filter a text file and change a specific character for another. In this case the ” character becomes the ‘ character using the magic of the sed command. Since I am using a character the shell also uses I have to escape it out so the command will work. This can make for a complex regex when you are building a Regular Expression.

[ john@3.2.0-2-486 ]
[ Jobs 0.PWD: /media/Stuff/Windows.bash 4.2.20. ] [ 18 ]
[ 21:50:38 ]
[ $ ]-> cat new.ps1 | sed "s/\"/\'/gi;"
Write-Host 'This script prints information about your PC.' -foregroundcolor 'green'
Write-Host 'BIOS Information.'
$name = Read-Host 'Please enter a computer name.'
Get-WMIObject Win32_BIOS `
�computername $name
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $name #| Format-Table -Property IPAddress

Here is another example. Substituting one character for a whole word. The sed command can manage this easily.

[ john@3.2.0-2-486 ]
[ Jobs 0.PWD: ~/Documents.bash 4.2.20. ] [ 27 ]
[ 21:55:47 ]
[ $ ]-> uname -a | sed "s/\#/Number: /gi;"
Linux debian-mint 3.2.0-2-486 Number: 1 Mon Mar 5 00:55:40 UTC 2012 i686 GNU/Linux

To list all jpg files in a folder that start with “IMG” and end with the “*.jpg” extension use this command.

ls -hula IMG**.jpg

And you will get this output.

-rw-r--r-- 1 john john 293K  08-10-12 02:15 pm IMG-20121008-00046.jpg
-rw-r--r-- 1 john john 295K  11-10-12 10:07 pm IMG-20121008-00047.jpg
-rw-r--r-- 1 john john 278K  11-10-12 10:07 pm IMG-20121008-00048.jpg

if you further refine the command and add more to it; then you can refine the file listing down to one single file.

ls -hula IMG*47*.jpg

This time only one file is returned in the listing.

-rw-r--r-- 1 john john 295K  11-10-12 10:07 pm IMG-20121008-00047.jpg

This is a very good trick for listing only certain files in a directory listing using ls. Below is a further refinement of our ls command. The bash shell is very flexible when you are searching for files in a folder. Imagine if there are thousands of files in the folder and you are only looking for one specific file…

[ john@3.2.0-2-486 ]
[ Jobs 0.PWD: ~/Desktop.bash 4.2.20. ] [ 2 ]
[ 22:29:38 ]
[ $ ]-> ls -hula IMG*-*46*.jpg
-rw-r--r-- 1 john john 293K  08-10-12 02:15 pm IMG-20121008-00046.jpg

Here is another usage; looking for desktop screenshots from a specific time.

[flynn@flynn-grid-runner Pictures]$ ls -hula Screen*00:34:49*.png
-rw-rw-r-- 1 flynn flynn 304K  04-10-12 03:04 pm Screenshot from 2012-09-19 00:34:49.png

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

Leave a Reply