19

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

Input example:

Hellow1
Hellow2
Hellow3

Desired output

Hellow1
#Hellow2
Hellow3
  • 5
    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 08:02

9 Answers9

20

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

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

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

sed '2 s/./#&/' 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 (see How can I achieve portability with sed -i (in-place editing)?).

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/ { $0 = "#" $0 }; 1' input.txt >output.txt

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

Kusalananda
  • 333,661
5

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.

Brian C
  • 134
4

The general pattern to apply an s command to specific lines is [2addr]s... Where 2addr represents 0, 1 or 2 addresses. In other words, if you want to insert a # before lines that match a pattern:

sed -e '/pattern/s/^/#/'

If you want to do the replacement on line $n:

sed -e "${n}s/^/#/"  # (This requires that $n be a well formatted number)

If you want to match the range of lines $n to $m:

sed -e "${n},${m}s/^/#/"  # $n and $m must be well formed numbers.  eg '2'

If you want to match all lines between a line that matches pattern1 and a line that matches `pattern2:

sed -e '/pattern1/,/pattern2/s/^/#/' 
2

If you want to insert before lines 2 to 4 you can specify range.

sed -i -e '2,4s/^/# /' test.txt

If you want specific lines, not range, it should be in different expressions. (for example lines 2 and 4 but not 3.)

sed -i -e '2s/^/# /' -e '4s/^/# /' test.txt

For the OP, combining the two leading answers here:

credit: @kusalananda, @brian-c

sed -i -e 's/^Hellow2/# &/' my-file.txt

  • This will search for Hellow2 at the beginning of the line.
  • When the word matches, the entire match will be stored at ref 0 - which is accessed with & character.
  • We then replace that with:
    • hash sign
    • followed by a space
    • and then the entire match
JonnieJS
  • 121
  • 3
0

You can do it with awk:

awk '{if ($0 == "Hellow2") print "#"$0; else print $0}' yourfile > outputfile
0

Using Raku (formerly known as Perl_6)

raku -pe 's/^/#/ if $++ == 1;'  yourfile > outputfile

OR

raku -ne 'state $i; S/^/{"#" if $i++ == 1}/.put;'  yourfile > outputfile

OR

raku -ne 'if $++ == 1 {S/^/#/.put;} else {$_.put};'  yourfile > outputfile

Above are solutions coded in Raku, a member of the Perl-family of languages. Indexing in Raku starts from zero, so $++ == 1 or $i++ == 1 denotes the second line.

The first example uses the autoprinting -pe command line flags in conjunction with the familar "small-s" s/// operator. The second and third example use the non-autoprinting -necommand line flags in conjunction with the "large-S" S/// operator. The "large-S" operator has the property that it... "uses the same semantics as the s/// operator, except it leaves the original string intact and returns the resultant string... ."

Sample Input:

Hellow1
Hellow2
Hellow3

Sample Output:

Hellow1
#Hellow2
Hellow3

https://github.com/rakudo/rakudo/wiki/Running-rakudo-from-the-command-line
https://docs.raku.org/syntax/s$SOLIDUS$SOLIDUS$SOLIDUS
https://docs.raku.org/syntax/S$SOLIDUS$SOLIDUS$SOLIDUS
https://raku.org

jubilatious1
  • 3,195
  • 8
  • 17
0

Simple is Good

I am writing because I feel other answers are complicating things.

To replace the 2nd line:

printf 'Hellow1\nHellow2\nHellow3\n' | sed '2s/^/#&/'
Hellow1
#Hellow2
Hellow3

To replace Hellow2:

printf 'Hellow1\nHellow2\nHellow3\n' | sed 's/^Hellow2$/#&/'
Hellow1
#Hellow2
Hellow3
Weihang Jian
  • 1,227
0

If you require all even lines to be suffixed with a # then:

sed '2~2 s/./#&/' your file.txt
-1
sed -i 's/string/#&' file

string should be entered from starting