Substitution

Last update on May 8, 2009 08:56 AM by jak58
Published by netty5

Substitution





The substitute command:[ s. ]changes all occurrences of the regular expression into a new value.

Basic Substitution


Simple


1st match (each line) encountered only

sed 's/la/LA/' file.txt

Global


All occurrences (each line) encountered
sed 's/la/LA/g' file.txt

Targetted


Only the 2nd occurrence (each line) encountered
sed 's/la/LA/2' file.txt

Conditioned Substitution


Simplified


/explanation/ - Only if the line contains "explanation"
sed '/awk/ s/sed/SED/' file.txt

Number of line - Only line no "N"
sed '18 s/sed/SED/' file.txt

/Regex/ - Only if there is correspondence with the regular expression
sed '/^[ ][Ss]ed/ s/ed/ED/g' file.txt

Advanced


0,/regex/ - Only the 1st case found

sed '0,/sed/ s//SED/' file.txt

Bloc


/explanation1/,/explanation2/ - Only in between "explanation1 - explanation2"
sed '/start/,/End/ s/ed/ED/' file.txt # Only the 1st case found
sed '/Start/,/End/ s/ed/ED/g' file.txt # all case

8,13 - Only in between lines 8 to 13

sed '8,13 s/ed/ED/g' file.txt

We can also combined reason and line

sed '8,/End/ s/ed/ED/g' file.txt
sed '/Start/,13 s/ed/ED/g' file.txt


Or with "regex"
sed '/Start/,$ s/ed/ED/g' file.txt # to the end($)
sed '/^[ ][Ss]ed/,13 s/ed/ED/' file.txt

Advanced


Replace any text between 2 grounds excluding grounds
sed '/Start/,/End/{ /Start/b;/End/b; s/.*/SED - The Stream ÉDitor/; }' file.txt
Best answers for « Substitution » in :
Substitution cipher Show Substitution cipher Substitution cipher involves replacing one or more entities (generally letters) in a message with one or more other entities. There are several types of substitution cryptosystems: Monoalphabetic substitution involves...
Substitute the sound track of a video Show Substitute the soundtrack of a video You have a video and you want to replace the soundtrack with one of your creation for any personal reasons : Make l use the Windows Movie Maker (it comes with Windows, if this is not the case you...
Transposition ciphers Show Transposition encryption methods involve rearranging data to be encrypted to make them incomprehensible. This may mean, for example, geometrically re-ordering data to make them visually unusable. The Assyrian technique The Assyrian cipher...
Tips and tricks ShowTips and tricks Substitution Display Remove Miscellaneous Substitution Substitute "foo" with "bar" on each line Only the 1st occurrence sed 's / foo / bar /' For the 3rd case only sed 's/foo/bar/3' All occurrences sed...
APT-GET OR APTITUDE ? ShowAPT-GET OR APTITUDE ? What is aptitude? Why aptitude? What is aptitude? aptitude is a package manager with a text mode interface. But just as apt-get, it can also work from the command line. In fact, you can directly substitute...
Download Texter ShowTexter allows you to spare time by reducing keystrokes when typing. The application substitutes and/or extends an abbreviation; that depends on your own configuration. Contrary to similar programs on Windows, Texter works with all the applications...