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.
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).