Search This Blog

Showing posts with label sed. Show all posts
Showing posts with label sed. Show all posts

sed: replace string between two matching patterns

# settings.xml
# <settings><token>xxx</token></settings>

cat settings.xml | sed -e "s|<token>.*</token>|<token>yyy</token>|g"

# <settings><token>yyy</token></settings>



see also

sed replace string in a file contains slash

sed -i "" "s|AAA/BBB|AAA/CCC|g" input-file.txt
or
sed -i ".bak" "s|AAA/BBB|AAA/CCC|g" input-file.txt

Note:

  • We want in place update to the file, so we need --in-place or -i option, and on Mac OS, the in place extension/suffix must be supplied even if it is a empty string: -i ""
  • We cannot use / as the delimiter

sed: replace formatted numbers

Example remove date string:
s="1234 2007-12-21 11:22:30 1.txt"
s=$(echo $s | sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\s*//')
echo $s
# 1234 1.txt

Sed: multiple substitutes in one sed command

Delimit commands with semicolon:
echo "John like apples." | sed 's/apple/pear/;s/John/David/'
Another example, remove leading and trailing quotes:
echo "'Sed is cool.'" | sed 's/^'//;s/'$//'

Shell scripting: enclose all lines with quotes

  • Using awk:
    awk '{print "\""$0"\""}' input.txt
    
    or if ignore empty lines:
    awk 'NF {print "\""$0"\""}' input.txt
    
  • Or using sed:
    sed -i.bak 's/^/"/; s/$/"/' input.txt
    
    or if ignore empty lines:
    sed -i.bak 's/^..*$/"&"/' input.txt
    

Shell Scripting: delete blank lines from a text file using sed

Delete empty lines using sed:
sed -i.bak '/^\s*$/d' 1.txt

shell script: insert line at the beginning of a text file

On linux:
sed -i -e '1s/^/Here is the first line.\n&/g' file.txt



On Mac OS, the above does NOT work. You have to use the following instead:
sed -i -e '1s/^/Here is the first line.\'$'\n/g' file.txt

see also

sed: append string to the end of the matching line

  • The orginal file file.txt:
    abc
    cde
    efg
  • Apend 123 to the line: cde. The expected result text should be:
    abc
    cde 123
    efg
  • The sed command:
    sed '/^cde/ s/$/ 123/' file.txt

sed: insert or append line after matching line

  • Original text file: 1.txt:
    abc
    def
    ghi
  • To insert line before line "abc":
    sed -i '/^abc/ i 111' 1.txt
    the result will be:
    111
    abc
    def
    ghi
  • To append line after line "abc":
    sed -i '/^abc/ a 111' 1.txt
    the result will be:
    abc
    111
    def
    ghi

sed: how to delete/skip empty/blank lines

  • To delete/skip the empty/blank lines:
    sed '/^$/d' inputFile
    
  • To delete/skip the lines that are empty or contain only spaces:
    sed '/^[[:space:]]*$/d' inputFile
    

sed: how to skip/delete lines starts with #

  • To delete/skip the lines starts with #:
    sed '/^#/d' inputFile
    
  • To delete/skip the lines where the very first non-space character is #:
    sed '/^[[:space:]]*#/d' inputFile
    

Edit a text file using sed

  • Replace a word
    • text.txt file contains following line:
      ios
      
    • Run the following command to replace ios with android:
      sed -e "s/ios/android/g" -i .bak text.txt
      
  • Append a word
    • text.txt file contains following line:
      leopard
      
    • Run the following command to append lion after leopard:
      sed -e "s/leopard/& lion/g" -i .bak text.txt
      
  • Append a token to each line
    • Run the following command to append 0 to each line:
      sed -e "s/$/ 0/g" -i .bak text.txt