D
D
Digsecman2021-07-19 12:01:46
linux
Digsecman, 2021-07-19 12:01:46

How to use awk to insert # on line 647?

Hello!
Can you please tell me what should be the syntax?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Shitskov, 2021-07-19
@Zarom

AWK
awk '{sub(/^/, "#", 647)}'
SED
sed -i '647s/^/#/' file.txt

X
xotkot, 2021-07-19
@xotkot

How to use awk to insert # on line 647?

paste where? beginning, end, middle?
if at the beginning, then:
awk -i inplace 'NR==647{print "# "$0}NR!=647' a.txt

where we write the changes (-i inplace) directly to the incoming file, on line (NR) 647, adding a hash with a space ("# ") in front of the entire line ($ 0), if desired, without a space ("#"). The remaining lines (NR!=647) we just print without changes.

X
xibir, 2021-07-19
@xibir

awk '{ if (NR == 647) print "#"$0; else print; }' < input.txt > output.txt

but sed is easier
sed -ri '647s/.*/#&/' input.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question