33

I want to add hashes to all the lines in a regular text file. I'm fine with both the use of terminal and GUI—I just need to get it done.

Oxwivi
  • 17,849

8 Answers8

57

You can use sed to do that:

sed -i.bak 's/^/##/' file

This replaces the start of the line (^) with ##.

With the -i.bak switch, sed edits the file in-place, but creates a backup copy with extension.bak.

dv3500ea
  • 37,204
  • 1
    +1 This is the first time I encounter this backup method. I always did an inline replace directly: sed 's/^/##/' -i file. In this case, this would be preferred since it cannot go wrong. In other cases, this command can be combined with diff -u file.bak file to see the differences (if necessary, pipe it through less). If it's confirmed to work, the backup can be removed. Otherwise it could be restored with a simple mv file.bak file. – Lekensteyn Jul 30 '11 at 08:28
  • 1
    Elegant. I was thinking about something like sed 's/^\(.*\)$/##\1/', but this is much nicer. – arrange Jul 31 '11 at 08:54
  • Nice one.. I was thinking about suggesting Vim to do this... – Sathish Aug 06 '11 at 13:28
  • This helped me convert a block list file :) Thank you 1+ – Neil Nov 22 '15 at 04:25
8

Here is a solution to this problem using perl

perl -e 'while (<>) {print "##$_"}' < infile > outfile
beav_35
  • 384
  • 2
    The -p switch is also useful: perl -pe 's/^/##/' infile > outfile. (There's also the -i[extension] switch for replacing the target file in-place.) http://perldoc.perl.org/perlrun.html#%2a-p%2a – Jukka Matilainen Jul 31 '11 at 07:51
7

While we are at it:

gawk -i inplace '{print "##"$0}' infile

This uses the (comparatively new) inplace editing plugin for GNU awk 4.1.0+.

muru
  • 197,895
  • 55
  • 485
  • 740
5

Here's a bash way:

while read -r; do printf '##%s\n' "$REPLY"; done < infile > outfile

(In the bash shell, running read -r with no other arguments works like IFS= read -r REPLY.)

This is stylistically inspired by beav_35's perl solution, which I admit probably runs much faster for huge files, since perl may be expected to be more efficient than a shell when it comes to text processing.

Eliah Kagan
  • 117,780
4

sed -i is not POSIX-standard, so if you are a purist you will want to use ed:

printf ",s/^/##/\nw\nq" | ed -s file.txt
fkraiem
  • 12,555
  • 4
  • 35
  • 40
3

Here's an easier perl way than presented elsewhere:

perl -pi -e 'print "##"' YOURFILEHERE

This (ab)uses the fact that perl -p prints the line after executing the command given in -e.

abligh
  • 375
0

You can use Vim in Ex mode:

ex -sc '%s/^/##/|x' file
  1. % select all lines

  2. s substitute

  3. x save and close

Zombo
  • 1
0

Can be done with python's mapping function and redirecting stdin:

$ cat input.txt                                                                                                          
lorem ipsum
quick brown fox
hello world
$ python -c 'import sys;print "".join(map(lambda x: "##"+x,sys.stdin.readlines()))'  < input.txt                         
##lorem ipsum
##quick brown fox
##hello world

Save the output to new file and use it instead of original

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497