Last active
April 19, 2023 22:02
-
-
Save cpuuntery/4d1875b9c02b9a387686faee8d0e3fad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sed -i 's/original/new/g' file.txt | |
SED WILL NOT WORK WITHOUT THE SINGLE QUOTES | |
JUST LIKE GREP USE SINGLE QUOTE AND NEVER USE DOUBLE QUOTES | |
There is only one way to replace multiple strings in a file using sed. Or a multiple file s if you use the find command. | |
sed -i -e 's/abc/new/g' -e 's/nothing/new/g' theisatest.txt | |
You can use the following commands when piping from less or cat, but you cannot overwrite the file, you can just write the output to another file | |
1- Pipe sed into another sed | |
sed s/1/0/ | sed s/true/false/ (just piping) | |
sed 's/abc/new/g' theisatest.txt | sed 's/nothing/new/g' (piping file no need for cat or less. The name of the file must only be added at the beginning of the pipe) | |
sed 's/abc/new/g' theisatest.txt | sed 's/nothing/new/g' > theisatest1.txt | |
sed 's/abc/new/g' theisatest.txt | sed 's/nothing/new/g' | tee th444eisatest1.txt | |
>> append at the end of file | |
> overwrite the file | |
2-separate the patterns using -e | |
sed -e 's/1/0/g' -e 's/true/false/g' | |
-i argument makes sed Overwrite the file. But when piping it is useless, so do not use it when piping | |
Everything between the start of range, and the end of range can be changed | |
sed is greedy, it does not support the non-greedy [?] as a workaround you need to be very specific about the end of range | |
if the start of range and end of range are not at the beginning of the line [.*] will help you a lot | |
sed '/start_of_range/,/end_of_range/ s/find/replace/' file | |
sed '/start_of_range/{:a;n;/^end_of_range/!ba;s#"find"#"replace"#}' file | |
use the -E flag when dealing with complex regex. And a fun fact, grep also has -E but it is still weaker than the -P | |
-e is only used when specifying two regexes. | |
-e only expect another -e after it, so you can not use the -E with -e | |
and if you are replacing in a file, the first flag must be -i | |
sed -e 's/string1/string2/g' -e 's/string4/string5/g' | |
sed does not support all the “character classes”, or also called “characters sets” | |
you will need to use the alternative form | |
here is the table | |
note that dot [.] Wildcard is supported. i will not write the alternative form to \s or \S because it is so long and \s or \s are kind of niche | |
\d Digit [0-9] - matches a digit | |
\D Non-digit [^0-9] - matches all but a digit | |
\w Word [a-zA-Z0-9_] - matches a word character | |
\W Non-word [^a-zA-Z0-9_] - matches all but a word character | |
If you want to use back-references in sed the flag -E is a must | |
in other regex engines $1 means return the first group | |
in sed it is \1 like this | |
echo "one two three four" | sed -E 's/(\w+) (\w+)/\2 \1/g' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment