TransWikia.com

insert character in the beginning of specified line in file

Unix & Linux Asked by user210015 on December 15, 2021

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

7 Answers

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.

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

Answered by jubilatious1 on December 15, 2021

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/^/#/' 

Answered by William Pursell on December 15, 2021

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

Answered by JonnieJS on December 15, 2021

sed -i 's/string/#&' file

string should be entered from starting

Answered by raghvendra gupta on December 15, 2021

To insert a # on the line 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

Answered by Kusalananda on December 15, 2021

You can do it with awk:

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

Answered by Wissam Roujoulah on December 15, 2021

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.

Answered by Brian C on December 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP