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.
jason@jason-desktop:~/Documents$ sed "s/\"/\'/gi;" setup-ad-server.ps1 # # Windows PowerShell script for AD DS Deployment # Import-Module ADDSDeployment Install-ADDSForest ` -CreateDnsDelegation:$false ` -DatabasePath 'C:\Windows\NTDS' ` -DomainMode 'Win2012' ` -DomainName 'john.local' ` -DomainNetbiosName 'JOHN' ` -ForestMode 'Win2012' ` -InstallDns:$true ` -LogPath 'C:\Windows\NTDS' ` -NoRebootOnCompletion:$false ` -SysvolPath 'C:\Windows\SYSVOL' ` -Force:$true |
Here is another example. Substituting one character for a whole word. The sed command can manage this easily.
jason@jason-desktop:~/Documents$ uname -a | sed "s/\#/Number: /gi;" Linux jason-desktop 4.10.0-35-generic Number: 39-Ubuntu SMP Wed Sep 13 07:46:59 UTC 2017 x86_64 x86_64 x86_64 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 |