Vigneshwaren
Vigneshwaren

Reputation: 1393

Sed to comment out a number of files

I have a file with kernel configuration variables. I would like to comment out the last 130 lines.

I am aware that sed does an inline replace, how do I use this command in conjuction with tail to comment out the last 130 characters.

Thanks in advance.

tail -n 130 <file-name> | sed -i ... #I am clueless beyond this point

Upvotes: 1

Views: 141

Answers (4)

Farvardin
Farvardin

Reputation: 5424

to change from line 130 to end try this :

sed '130,$s/^/#/'

this add a # at the start of line 130 to end.

to change last 130 lines do this :

tac file | sed '1,130s/^/#/'  | tac

Upvotes: 5

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed -i ':a;${s/^/#/Mg;b};N;s/\n/&/130;Ta;P;D' file1 file2 file...

Upvotes: 0

Jotne
Jotne

Reputation: 41460

Here is how to comment out last 130 line with awk

awk '{a[NR]=$0} END {for (i=1;i<=NR;i++) {if (i>NR-130) a[i]="#"a[i];print a[i]}}' file

Here is another shorter awk

awk 'FNR==NR {a=NR;next} FNR>a-130 {$0="#"$0}1' file{,}

The file{,} is the same as file file

Upvotes: 3

user3442743
user3442743

Reputation:

As i cant add a comment to طاهر answer ive had to make my own.

Your answer comments out everything from line 130 till the end. Wasnt the question for 130 lines from the end, not 130 lines until the end ?

If it was in a script then using

FROM=$(wc -l < file)
(( FROM = FROM - 130 ))

and then

sed $FROM',$s/^/#/' < file

would work.

Upvotes: 1

Related Questions