insert # in a file (139 Views)
Reply
Frequent Advisor
rhansen
Posts: 47
Registered: ‎09-10-2009
Message 1 of 11 (139 Views)
Accepted Solution

insert # in a file

Hello,

I have a text file that has hundreds of line. I need to comment out the first 150 lines by adding a "#".

Can someone let me know how do it in one shot.

Thanks.
Please use plain text.
Honored Contributor
Rita C Workman
Posts: 3,791
Registered: ‎08-03-2000
Message 2 of 11 (139 Views)

Re: insert # in a file

How about

cat | sed s'/.*/#&/' >>

Rita
Please use plain text.
Honored Contributor
Rita C Workman
Posts: 3,791
Registered: ‎08-03-2000
Message 3 of 11 (139 Views)

Re: insert # in a file

oops sorry ignored the 150 lines..

how about...

sed -n '1,150p' | sed s'/.*/#&/' >>

/Rita
Please use plain text.
Trusted Contributor
Sagar Sirdesai
Posts: 385
Registered: ‎04-25-2005
Message 4 of 11 (139 Views)

Re: insert # in a file

sed '1,150s/^/#/' file > newfile
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 11 (139 Views)

Re: insert # in a file

Hi:

If you prefer 'sed', Sagar's solution is good.

IF you want to use Perl and update the file in-place while preserving a backup copy as ".old", you could do:

# perl -pi.old -e '1..150 and s/^/#/' file

The GNU 'sed' also offers in in-place file update. HP's 'sed' does not.

Regards!

...JRF...
Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,078
Registered: ‎02-23-2005
Message 6 of 11 (139 Views)

Re: insert # in a file

> [...] update the file in-place [...]

Of course, if you'd like to check the work
before replacing the original file, then
in-place may not be the ideal method to use.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,877
Registered: ‎03-06-2006
Message 7 of 11 (139 Views)

Re: insert # in a file

>I need to comment out the first 150 lines by adding a "#".

If you are only doing it once, you can use vi, similar to Sagar's sed:
:1,150s/^/#/
Please use plain text.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 8 of 11 (139 Views)

Re: insert # in a file

here is a solution with awk:

awk '{if (NR < 151) print "#"$0; else print $0}' filename

****
Unix operates with beer.
Please use plain text.
Esteemed Contributor
T. M. Louah
Posts: 382
Registered: ‎11-17-2000
Message 9 of 11 (139 Views)

Re: insert # in a file

Great tips, thx
Little learning is dangerous!
Please use plain text.
Frequent Advisor
rhansen
Posts: 47
Registered: ‎09-10-2009
Message 10 of 11 (139 Views)

Re: insert # in a file

Thanks for the replies.
Please use plain text.
Trusted Contributor
Sagar Sirdesai
Posts: 385
Registered: ‎04-25-2005
Message 11 of 11 (139 Views)

Re: insert # in a file

Hi rhansen,

I hope we deserve some for our answers.
We encourge you to assign points

Sagar
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation