sed
Four types of sed scripts
1. Multiple Edits to the Same File
2. Making Changes Across a Set of Files
3. Extracting Contents of a File
4. Edits To Go
Substitute [address]s/regexp/replacement/[flags]
The ‘s’ command (as in substitute) is probably the most important in ‘sed’ and has a lot of different options. The syntax of the ‘s’ command is ’s/REGEXP/REPLACEMENT/FLAGS'.
The ‘/’ characters may be uniformly replaced by any other single character within any given ‘s’ command.
ie ’s:REGEXP:REPLACEMENT:FLAGS’ or ’s|REGEXP|REPLACEMENT|FLAGS’ etc
Flags
- n
- A number indicating the replacement should only be done for the nth occurence of the regexp.
- g
- Make changes globally on all occurrences in the pattern space. Normally only the first occurrence is replaced.
- p
- Print the contents of the pattern space
- w file
- Write the contents of the pattern space to file.
Flags can be used in combination where it makes sense. For instance, gp makes the substitution globally on the line and prints the line.
Replacement metacharacters
- &
- Replaced by the string matched by the regular expression.
- \n
- Matches the n th substring (n is a single digit) previously specified in the pattern using “\(” and “\)”.
ExampleGroup 'The syntax of the s command is ‘s/regexp/replacement/flags’'
# https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/
# ignore case sed -i 's/find/replace/gi' file.txt
text="unix is great os. unix is opensource. unix is free os."
lines=$(cat << END
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
END
)
Example 'replace first occurence of regexp with replacement'
When call echo $(sed 's/unix/linux/' <<< "$text")
The output should eq 'linux is great os. unix is opensource. unix is free os.'
End
Example 'replace second occurence of regexp with replacement'
When call echo $(sed 's/unix/linux/2' <<< "$text")
The output should eq 'unix is great os. linux is opensource. unix is free os.'
End
Example 'replace third occurence of regexp with replacement'
When call echo $(sed 's/unix/linux/3' <<< "$text")
The output should eq 'unix is great os. unix is opensource. linux is free os.'
End
Example 'replace all occurences of regexp with replacement'
When call echo $(sed 's/unix/linux/g' <<< "$text")
The output should eq 'linux is great os. linux is opensource. linux is free os.'
End
Example 'replace from the second occurence of regexp with replacement'
When call echo $(sed 's/unix/linux/2g' <<< "$text")
The output should eq 'unix is great os. linux is opensource. linux is free os.'
End
Example 'paranthesize first character in each word'
When call echo $(sed 's/\(\b[a-z]\)/\(\1\)/g' <<< "$text")
The output should eq '(u)nix (i)s (g)reat (o)s. (u)nix (i)s (o)pensource. (u)nix (i)s (f)ree (o)s.'
End
Example 'select a specific line number'
When call echo $(sed '3 s/unix/linux/' <<< "$lines")
The output should eq "unix is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
End
Example 'select a range of line numbers'
When call echo $(sed '1,3 s/unix/linux/' <<< "$lines")
The output should eq "linux is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
End
Example 'select from second to last lines'
When call echo $(sed '2,$ s/unix/linux/' <<< "$lines")
The output should eq "unix is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
End
End
Delete lines
‘/REGEXP/d’
Create a newline after each newline as needed by md
sed G
Insert lines
‘a TEXT’
$ seq 3 | sed '2a hello'
Transliterate ‘y/SOURCE-CHARS/DEST-CHARS/’
Transliterate any characters in the pattern space which match any of the SOURCE-CHARS with the corresponding character in DEST-CHARS.
See the ‘tr’ command from GNU coreutils for similar functionality.
Print (as in grep) ‘/REGEXP/p’
Range addresses
To extract YAML between lines starting and ending with — in Hugo index.md files:
sed -n '/---/,/---/{//!p}' ./content/events/whatevern/index.md
Printing
ExampleGroup 'The syntax of the s command is ‘s/regexp/replacement/flags’'
# https://www.geeksforgeeks.org/sed-command-linux-set-2/
lines=$(cat << END
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
END
)
Example 'G add a newline and 80 spaces to end of line'
When call echo $(sed -n '4p' <<< "$lines")
The output should eq "Don’t hesitate to ask for love & don’t hesitate to show love too."
End
End
Deleting
ExampleGroup 'The syntax of the s command is ‘s/regexp/replacement/flags’'
# https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/
some_text="operating system"
# Note variables can hold text spread over multiple lines and these can contain variables to expand
lines="unix is great os. unix is opensource. unix is free os.
learn $some_text.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
Example 'delete first line'
When call echo "$(sed '1d' <<< "$lines")"
The output should eq "learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
End
Example 'delete last line'
When call echo "$(sed '$d' <<< "$lines")"
The output should eq "unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose."
End
Example 'delete range of lines'
When call echo "$(sed '2,3d' <<< "$lines")"
The output should eq "unix is great os. unix is opensource. unix is free os.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
End
Example 'delete lines containing pattern'
When call echo "$(sed '/linux/d' <<< "$lines")"
The output should eq "unix is great os. unix is opensource. unix is free os.
learn operating system.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful."
End
End
Inserting
ExampleGroup 'The syntax of the s command is ‘s/regexp/replacement/flags’'
# https://www.geeksforgeeks.org/sed-command-linux-set-2/
# insert 3rd in third line sed '3i\3rd line' file.txt
# append after 4th line sed '4a\Let's append text' file.txt
lines=$(cat << END
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life, experience will help you differentiating right from wrong.
END
)
Example 'G add a newline and 80 spaces to end of line'
When call echo $(sed 'G' <<< "$lines")
The output should eq "life isn't meant to be easy, life is meant to be lived. Try to learn & understand something new everyday in life. Respect everyone & most important love everyone. Don’t hesitate to ask for love & don’t hesitate to show love too. Life is too short to be shy. In life, experience will help you differentiating right from wrong."
End
End
Other
The REPLACEMENT can contain ‘\N’ (N being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the Nth ‘(’ and its matching ‘)’.
Also, the REPLACEMENT can contain unescaped ‘&’ characters which reference the whole matched portion of the pattern space.
‘\L’ Turn the replacement to lowercase until a ‘\U’ or ‘\E’ is found,
‘\l’ Turn the next character to lowercase,
‘\U’ Turn the replacement to uppercase until a ‘\L’ or ‘\E’ is found,
‘\u’ Turn the next character to uppercase,
‘\E’ Stop case conversion started by ‘\L’ or ‘\U’.
ExampleGroup 'common uses of sed'
Example 'title case lower case text'
text=$(echo "this is a test" | sed 's/[a-z]*/\u&/g')
When call echo "$text"
The output should eq 'This Is A Test'
End
End