I have a set of files in a directory that I have to read and replace the first occurrence of a number with another dummy number. This is what I have so far but it does not seem to work. The files have lot of other data in each row and each data element is separated by ,@,
for file in *alpha* *beat* *kappa*
do
awk -F",@," '{ print $1 }' $file > sed -i 's/${1}/999/g' $file
done
Here in the example I am trying to replace the first number in each row of the file with a dummy number 999.
Thanks for the help. The script is working but is not able to change the contents in the file.
I am not sure if it has to do with any permissions, but when I ran the script and directed output to a file, I see the changed values, not in the original files for some reason.
How do I effect the changes in the files?
---------- Post updated at 12:50 PM ---------- Previous update was at 12:49 PM ----------
I'm not quite sure what the OP wants. The problem statement states that the desire is to change the first number, but the AWK is extracting the first field without regard to what it contains. Following the AWK lead, the following changes everything preceding the first comma.
The following modifies the file in-place. Proceed with caution.
Use alister's solution (but carrefully), no need for a temp file and your files won't have their inode changed (contrary to the proposed solution with the mv command).
You sure you want to append to the same file you're reading from? Even if you don't find yourself in a loop where AWK reads lines it wrote itself, you're not changing the original lines.