Need help to replace a pattern on specific line in a data file

Hi,

I want to replace specific pattern "-2.0000 2" by "1.0000 3" on a particular line (line #5) in a file 1.dat. I have about 50 more files similar to 1.dat in which I want to do this correction.

Can you please suggest how can I make change only at this particular line of a file and retain remaining file as it is.

$cat 1.dat
      1     4.619209   -2.737649   16.892469  -2.0000       2      10       1
      2    -4.132976    1.244385   16.892469  -2.0000       2      10       2
      3     7.536607    3.235402   16.892469  -2.0000       2      10       3
      4    12.885178   -2.737649   16.892470  -2.0000       2       9       4
      5    -7.050373   -0.248878   16.892469  -2.0000       2      12       5
      6     5.591674    1.244385   16.892469  -2.0000       2      10       6
      7    -7.536606   -0.248878   16.892469  -2.0000       2      11       7
      8    -5.591674    2.239894   16.892469  -2.0000       2       9       8
      9     1.215581   -2.737649   16.892469  -2.0000       2       9       9
     10     6.564140    3.235402   16.892469  -2.0000       2      10      10
$

required output

$cat 1.dat
      1     4.619209   -2.737649   16.892469  -2.0000       2      10       1
      2    -4.132976    1.244385   16.892469  -2.0000       2      10       2
      3     7.536607    3.235402   16.892469  -2.0000       2      10       3
      4    12.885178   -2.737649   16.892470  -2.0000       2       9       4
      5    -7.050373   -0.248878   16.892469  1.0000       3      12       5
      6     5.591674    1.244385   16.892469  -2.0000       2      10       6
      7    -7.536606   -0.248878   16.892469  -2.0000       2      11       7
      8    -5.591674    2.239894   16.892469  -2.0000       2       9       8
      9     1.215581   -2.737649   16.892469  -2.0000       2       9       9
     10     6.564140    3.235402   16.892469  -2.0000       2      10      10
$

I tried

sed -n '5p' 1.dat |"s/-2.0000       1/1.0000       3/" > 1.dat

, but this just print line #5 and do not give remaining content.

Thank you,
Anuj

awk 'NR==5{sub("-2.0000 2","1.0000 3",$0)}1' 1.dat > 1.fixed.dat
while read file
do
    ed -s "$file" <<-EOF
        5s/-2.0000 1/1.0000 3/
        w
        q
    EOF
done < list

@bartus11

Thank you , it works !

---------- Post updated at 01:15 PM ---------- Previous update was at 01:12 PM ----------

@Don

Thank you, for your reply. But, it gives me error :

"syntax error: unexpected end of file"

Am I making some mistake.

here is the script I used

$ cat replace.sh 
#!/bin/bash

while read file
do
    ed -s "$file" <<-EOF
        21s/-2.0000       2/1.0000       3/
        w
        q
    EOF
done < list

---------- Post updated at 01:29 PM ---------- Previous update was at 01:15 PM ----------

Anyway, thank you guys ! Its working now

Sorry,
I had some cut and paste errors. The script I supplied should have been:

while read file
do
        ed "$file" <<-EOF
                5s/-2.0000 2/1.0000 3/
                w 
                q
        EOF
done < list

This should work with ksh, bash, or any POSIX conforming shell.

The <space>s at the start of the lines in the while loop should have been <tab>s. (That kept the EOF from being recognized as the end of the here-document.)

The s/-2.0000 1/1.0000 3/ was copied from the sed you said you used in the 1st message in this thread. The 1st part of your problem statement had the desired replacement listed correctly; I should have read what you said instead of copying your code. (That would have prevented a successful replacement.)

In your quote above you said you used the ed command:

21s/-2.0000       2/1.0000       3/

but I don't know where the 21 at the start of that line came from. I had 5 there instead of 21 because you said you wanted to make the change on line 5 in all of the files you're going to process. Also note that in this substitution you're looking for eight spaces between fields, but the input file you provided only has a single space between fields. If the number of spaces in your substitute command doesn't match the number of spaces in the files you're editing, the substitution will fail (in awk, ed, and sed).

Hope this helps,
Don

@Don

Thank you.

Yes, I made some changes in line#21, so I put it there.

If Perl is an option, then :

perl -i.bak -pe 's/-2.0000       2/ 1.0000       3/ if $.==5; close ARGV if eof' *.dat

tyler_durden