Bookmark and Share

Add a newline character at the end of file if there is not one

GNU sed:

$ sed -i.bak -e '$a\' file

POSIX sed, such as the default on macOS:

$ sed -i .bak -e '$a\' file

Remarks:

  • -i : edit file in place with backup created with given file extension

  • -e '$a\' : append editting command '$a\' to the list of commands

  • '$a\' :

    • $ matches the last end of line in the file
    • a append
    • \ newline character

Substituting text

Change all instances of foo to bar:

$ sed -i .bak -e 's/foo/bar/' *.txt

Change all instances of foo to bar even for foo that spread into multiple lines:

$ sed -i .bak -e 's/foo/bar/' *.txt

Uncommenting

Removing the pair of parentheses (e.g. (* and *) for Mathematica code) around comments:

$ sed -e 's/\(\* (.*(Head1|Head2).*)\*\)/\1/' file.old.txt >file.new.txt

<         (* {"Head1", "Value"} -> "value1",*)
<         (* {"Head2", "Value"} -> "value2",*)
---
>         {"Head1", "Value"} -> "value1",
>         {"Head2", "Value"} -> "value2",

Find files containing one or multiple lines of text starting with beginregex and ending with endregex

sed '/beginregex/,/endregex/!d' ./*.txt

Find lines matching regex1 followed by a line matching regex2 in file

sed -n -e '
    /regex1/ {
        N
        /\nregex2/ {
            s/\(regex1\)/\1/ p
        }
    }
' file

Remarks:

  • -n is to suppress default behavior of printing each line

  • -e indicates the following is a sed command

  • N indicates to concatenate the following line

  • p indicates to print the value of \1

  • \1 can be changed to something else if you want to print something dependent on the string matching regex1

For example, if the file contains

A1
A2
B1
B2
A3
A4
B3

The above program with regex1=A and regex2=B will print

A2
A4

Substitute string1 with string2 in all files with names matching pattern in a directory recursively

find . -type f -name *foobar.txt -print0 | xargs -0 sed -i .bak -e 's/string1/string2/'

Extract parts related to the -a option from man page of git

git help commit | sed -n '/-a,/,$p' | sed '/^$/,$d'

References

External links

blog comments powered by Disqus