Home tmade.de

Home Wiki

sed

Replace 12345 with 678910 in file mytest.txt and output to edited.txt

sed 's/12345/678910/g' mytest.txt > edited.txt                 #Replace all occurencies
sed 's/12345/678910/' mytest.txt > edited.txt                  #Replace single occurency
sed -i 's/12345/678910/g' mytest.txt                           #Inline replace of all occurencies
sed -i 's/12345/678910/' mytest.txt                            #Inline replace single occurency
sed -i "s/V201/V210/" *_c.par                                  #Inline replace of all occurencies
sed -i 's!/path/to/replace!/replaced/path!g'                   #Replace a Path with "/" (delimter is "!")
sed -i '/disable/s|^|#|' /path/to/file                         #Add "#" to beginning of line, if searchstring (disable) matches
mv /folder/$i $(echo /folder/$i | sed 's/.lock$//');           #Rename a file saved in var "i" and remove ".lock" from filename

Other examples:

sed s/"if \[ \$vmdb_answer_OPEN_VM_COMPAT = 'yes' \] ; then"/"if \[ \"\$vmdb_answer_OPEN_VM_COMPAT\" = 'yes' \]; then"/g /etc/init.d/rc3.d/S01vmware-tools -i
NETMASK=$(/sbin/ifconfig eth0 | grep Mask | awk '{print $4}'|sed 's/Mask://')

Replace string with “space” oder several “spaces”:

Example: “this string will be replaced”

sed -i 's/this\sstring\swill\sreplaced/replaced_string/g' *                #replace "this string will be replaced" to "replaced_string"
sed -i "s/ssl_protocols\s\sSSLv3;/ssl_protocols  SSLv3 TLSv1.2;/g" *.conf  #replace "ssl_protocols  sSSLv3;" to "ssl_protocols  SSLv3 TLSv1.2;"

output to screen/console

sed "s/12345/678910/" mytest.txt 

or

sed -e "s/12345/678910/" mytest.txt

Replace globaly recursive:

grep -rl "domain.de/subfolder" /somedir | xargs sed -i "s/domain.de\/subfolder/replace_string/g"

Replace lines beginning with “;” or “0-9”:

cat file | sed -i '/^[;0-9]/s|^|#disabled#|'

or

cat file | sed 's/^\(;\|[0-9]\)/#disabled#/'

Search lines in file:

sed -n "$i"p file                                    #search line "i" in file
sed -n "1"p file                                     #search line "1" in file
sed -n '10,20p' file                                 #search line 10-20

Search pattern in files:

sed -n '/PATTERN1\|PATTERN2\|PATTERN3/p' FILE

Delete:

sed -i '/search-string/d' file

Search and add two lines

Before sed:

            - name: IP
              value: "autodetect"

After sed:

            - name: IP
              value: "autodetect"
            - name: IP_AUTODETECTION_METHOD
              value: "interface=ens18"

Code:

INTERFACE="ens18"
sed -i 's/value: "autodetect"/value: "autodetect"\n            - name: IP_AUTODETECTION_METHOD\n              value: "interface='${INTERFACE}'"/g' ${CALIVERS}

Regular Expression

Explication of options:

s - substitute
/ - beginning of patter to match
^ - The null character at the start of the line
\(....\) - store this in the pattern buffer
[0-9]* - match any number of occurrences numbers in the range 0-9
[:] - match the ":" character
.* - match any number of any characters (the rest of the line)
/ - end on the match patter and beginning on the replace pattern
\1 - the first entry in the pattern buffer ( what was stored with \(...\) )
/ - end of the replace pattern
p - print
^B[0-9]{3}[ABC][0-9]+                                                            #must beginn with "B" followed from 3 digits from "0-9", followed from "A", "B" or "C", folowed from "0-9", followed from one or more signs
shell/sed.txt · Last modified: 2024/10/09 16:24 by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki