Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 22 down vote accepted

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.

share|improve this answer
    
+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 8:28
    
Elegant. I was thinking about something like sed 's/^\(.*\)$/##\1/', but this is much nicer. –  arrange Jul 31 '11 at 8:54
    
Nice one.. I was thinking about suggesting Vim to do this... –  Sathish Aug 6 '11 at 13:28
add comment

Here is a solution to this problem using perl

perl -e 'while (<>) {print "##$_"}' < infile > outfile
share|improve this answer
    
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.) perldoc.perl.org/perlrun.html#%2a-p%2a –  Jukka Matilainen Jul 31 '11 at 7:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.