Comment out every line in a file

From raju

Description

The idea here is to comment out every line in a file. Consider for example

    $cat foo.txt
    foo
    bar
    baz
    qux
    quux
    

We want

    # foo
    # bar
    # baz
    # qux
    # quux
    

Solution using awk

    $awk '{printf "# "; print}' foo.txt
    # foo
    # bar
    # baz
    # qux
    # quux
    

Solution using sed

    $sed -e 's/^/# /g' foo.txt
    # foo
    # bar
    # baz
    # qux
    # quux
    

You can also do

    $sed -e 's/\(.*\)/# \1/g' foo.txt
    # foo
    # bar
    # baz
    # qux
    # quux
    


To comment out only some lines but not all

    $sed -e '2,4s/^/# /g' foo.txt
    foo
    # bar
    # baz
    # qux
    quux
    

tags | prepend each line with something

Ref: