How to rewrite a line in a file

Hi

Can anyone tell me how can rewrite a line in a file by using line number? I tried to use sed by failed to do so.
For example

HELLO
179.390 111.560
HOW TO DO
WHAT TO DO

to become

HELLO
200 3000
HOW TO DO
WHAT TO DO

To replace line 2 :

sed '2c\
200 3000' infile > outfile

With awk:

awk 'NR==2 {print "200 3000"; next} 1' infile > outfile

Jean-Pierre.

Here is another way of doing it:
To change line 2:

sed '2s/.*/200 3000/' infile

Thanks everyone for valuable suggestions.

I tried both commands suggested by aigles but it doesnt work. I got errors which indicating syntax error if I am not mistaken.
However, Shell Life commands work well.

I wish to extend my questions base on the question I posted the other day.
How about only change the 2nd word on the second line of the file? For example
HELLO
179.390 111.560
HOW TO DO
WHAT TO DO

to become

HELLO
179.390 50000
HOW TO DO
WHAT TO DO

The two commands work fine on my AIX box.
What errors you got ?

To change word 2 on line 2, you can do :

awk 'NR==2 {$2=50000; print ;next} 1' infile > outfile

Jean-Pierre.

awk 'NR==1 {$2=50000;print;next}{print}' file

The error that I got when run awk "NR==3 {print "200 3000"; next} 1" unit.dat
is as below.

Syntax Error The source line is 1.
The error context is
NR==3 {print >>> 200 <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

is that indicating syntax error?

Hi Jean-Pierre

I thought " and ' are refering to the same thing in unix. After changing " to ', the problem solved. Sorry for all the trouble.

hi

can I use awk command to change word 2 on line 2 and word 3 on line 3 into 777 and 555 respectively (for example)? if yes, how should I write the awk syntax?

awk ' { if (NR == 2) { $2 = 777 } if (NR == 3) { $3 = 555 } print } ' filename

Hi

may I know whether the following syntax is correct or not? if not, what should be the correct one?

awk ' { if (NR == 2) { $2 = 777 $1 = 888 } if (NR == 3) { $3 = 555 } print } ' filename

Add a semicolon

awk ' { if (NR == 2) { $2 = 777 ;$1 = 888 } if (NR == 3) { $3 = 555 } print } ' filename

may i know how awk command count the number of words in a line?
for example
123,134,156 - is it counted as one words only? or 5 words?

If I wish to modify only 123 and 134 to 555 and 333 respectively (let says) in the line which has the pattern above, how should I go about to do it?

If you specify field separator as comma then here you have three fields or words.
$1 gives first field
$2 gives second field and so on.

$1 = 555
$2 = 333

This will modify the first and second field.

may i know how can I specify field seperator as comma? (or other sign like $, . and etc)?

use FS for input field separator and OFS for out put field separator, to set what you want as your field separator

Hi jean-pier,

   Can you please tell me why we need put 1 after this awk code ?
   awk 'NR==2\{print "200  300";next\}1'

awk ' { BEGIN { FS=","};if (NR == 2) { $2 = 777 ;$1 = 888 }; BEGIN { FS=" "};if (NR == 3) { $3 = 555 } print } ' filename

may i know whether the above syntax correctly defined the fiels seperator or not?
I want to define field seperator twice

You dont need to define field seperator twice.

awk ' BEGIN { FS=","} { if (NR == 2) { $2 = 777 ;$1 = 888 }; if (NR == 3) { $3 = 555 } print } ' filename

or

awk -F"," ' { if (NR == 2) { $2 = 777 ;$1 = 888 }; if (NR == 3) { $3 = 555 } print } ' filename

i field seperators that I want in line 2 and line 3 are different. In this case, I need to define field seperator twice, don`t I?

awk ' BEGIN { FS=","} { if (NR == 2) { $2 = 777 ;$1 = 888 }; if (NR == 3) { FS=" "; $3 = 555 } print } ' filename