linux - Bash : Replace entire line -


i using following line line number in specific string occurs :

nline=$(awk '/text/{ print nr; exit }' $1) echo "line = $nline" 

returns :

line = 78 

now, replace specific line other string using :

awk 'nr==$nline {$0="new text $2"} 1' test.xml 

where $2 param given bash script.

this command line works fine when enter directly terminal or when put parameter :

awk 'nr==78 {$0="new text data"} 1' test.xml 

but never works expected when parameters given command..

in addition, possible avoid print in terminal ? because when put > /dev/null @ end of line nothing appends.

for replacing line(s) can either use

sed or awk

specify line number in sed or nr (number of record) in awk shown in example below

awk 'nr==34 { sub("aaa", "bbb") }'

or use fnr (file number record) if want specify more 1 file on command line.

awk 'fnr==34 { sub("aaa", "bbb") }

' or

sed '34s/aaa/bbb/'

you can use variables replacement using $ sign1


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -