I need to insert character (#) in the beginning of specified line in a text file.

Input example:


Desired output

  • 3
    Don't post images of text – Kusalananda Jan 12 '17 at 7:45
  • Is the "specified line" the one in blue, or the one with the # already in front of it? Does the two sets of lines illustrate input and output or do they all belong to the same file? – Kusalananda Jan 12 '17 at 7:47
  • Hi,thanks for your comment. I meant input and output. – user210015 Jan 12 '17 at 7:59
  • The "specified line" the one with the # already in front of it – user210015 Jan 12 '17 at 8:00
  • How we're supposed to find the "specified line" ? Is it by line number ? Is it by specific text ? Is it by specific search pattern ? Please clarify your question – Sergiy Kolodyazhnyy Jan 12 '17 at 8:02

Your question is unclear. Assuming you're looking to comment out the specific text:

sed -i.bak 's/^\(Hellow2\)$/#\1/'

This will do an in-place replacement of any lines that exactly match the string "Hellow2", and replace them with a # followed by the line that was matched.

  • It works and save it to the file - Thanks a lot! – user210015 Jan 12 '17 at 8:15
  • 1
    The g modifier is not needed. Any pattern that is anchored with ^ (or $) can only match once anyway. – Kusalananda Jan 12 '17 at 8:18
  • @Kusalananda: Thanks, fixed this. Good point. – Brian C Jan 12 '17 at 8:35

To insert a # on the line with the word Hellow2, you may use sed like this:

sed 's/^Hellow2/#&/' input.txt >output.txt

To insert a # in the beginning of the second line of a text, you may use sed like this:

sed '2s/.*/#&/' input.txt >output.txt

The & will be replaced by whatever was matched by the pattern.

I'm avoiding using sed -i (in-place editing), because I don't know what sed you are using and most implementations of sed use incompatible ways of handling that flag.

Instead, do the substitution like above and then

mv output.txt input.txt

if you want to replace the original data with the result. This also gives you a chance to make sure it came out correctly.

Equivalent thing with awk:

awk '/^Hellow2/ { print "#" $0; next } { print }' input.txt >output.txt

awk 'NR == 2 { print "#" $0; next } { print }' input.txt >output.txt

You can do it with awk:

awk '{if ($0 == "Hellow2") print "#"$0; else print $0}' yourfile > outputfile
  • 1
    This can be shortened with awk '$0=="Hellow2"{print "#"$0;next};1' input.txt. The pattern before each code block {} can be treated as if statement already. 1 Just tells awk to print the line, that's all – Sergiy Kolodyazhnyy Jan 12 '17 at 8:07
  • @Serg Thanks for your info, you are totally right, but, i wanted it to be clear – Wissam Roujoulah Jan 12 '17 at 8:09
  • but it do not save it to the file.. how can I save it? – user210015 Jan 12 '17 at 8:11
  • @user210015 I edited my answer – Wissam Roujoulah Jan 12 '17 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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