Difference between revisions of "Sed"

From PKC
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
[https://man7.org/linux/man-pages/man1/sed.1.html sed] is a unix command for stream data editor.
[https://man7.org/linux/man-pages/man1/sed.1.html sed] is a unix command for stream data editor.


=Handling Special Characters=
=Special Characters=
When dealing with special characters, use a different set of separators. For example: when dealing with <code>'</code>, use the following command:
When dealing with special characters, use a different set of separators. For example: when dealing with <code>'</code>, use the following command:
  sed "/'/d" NoEmptyFiles.txt > NoSeparator.txt  
  sed "/'/d" OriginalTextualContent.txt > NoSeparator.txt  


==Two or more replacement rules==
If one needs to replace more than one set of characters, one can use the following expression:
sed -e "/'/d" -e "s/,/|/g" OriginalTextualContent.txt > NoSeparator.txt
Note the two <code>-e</code> prefixes. These prefixes defines the multiple substitution expressions.
For more explainations, see<ref>{{:Video/Learning Sed Is Beneficial For Linux Users}}</ref>
[https://youtu.be/EACe7aiGczw?t=656 Click here to see the portion that talks about special characters]
==Pattern matching with . * and $==
To remove some string after certain character, say <code>|</code>, see the following example:
echo "Module:734556565|Module:certainModuleName3" | sed "s/|.*$//"
Output:
Module:734556565
To learn more about these pattern defining instructions, see [https://en.wikipedia.org/wiki/Sed#Other_sed_commands Sed Commands].
=Quick Tutorial=
This 8 minute video<ref>{{:Video/How to use Sed substitutions, from basic to advanced - Yes, I Know IT ! Ep 08}}</ref> could significantly speed up your understanding of [[sed]].
<noinclude>
=References=
<references/>
=Related Pages=
[[Category:Unix Command]]
[[Category:Unix Command]]
</noinclude>

Latest revision as of 08:49, 28 January 2022

sed is a unix command for stream data editor.

Special Characters

When dealing with special characters, use a different set of separators. For example: when dealing with ', use the following command:

sed "/'/d" OriginalTextualContent.txt > NoSeparator.txt 

Two or more replacement rules

If one needs to replace more than one set of characters, one can use the following expression:

sed -e "/'/d" -e "s/,/|/g" OriginalTextualContent.txt > NoSeparator.txt 

Note the two -e prefixes. These prefixes defines the multiple substitution expressions.

For more explainations, see[1] Click here to see the portion that talks about special characters

Pattern matching with . * and $

To remove some string after certain character, say |, see the following example:

echo "Module:734556565|Module:certainModuleName3" | sed "s/|.*$//"

Output:

Module:734556565

To learn more about these pattern defining instructions, see Sed Commands.


Quick Tutorial

This 8 minute video[2] could significantly speed up your understanding of sed.


References

  1. DistroTube, ed. (Jun 25, 2021). Learning Sed Is Beneficial For Linux Users. local page: DistroTube. 
  2. Yes, I Know IT !, ed. (Mar 2, 2017). How to use Sed substitutions, from basic to advanced - Yes, I Know IT ! Ep 08. local page: Yes, I Know IT !. 

Related Pages