Replace first number of each line in a file with another number

Hi,

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.

Here is a sample file contents

123,@,x,@,y,@,45
453,@,x,@,y,@,23

Any help is appreciated. Thanks in advance !

@alister
... just another "ed" challenge for you :wink:

awk -F",@," '{sub($1,999); print}' file
1 Like

Are you willing to change the first number into the first row only or in every row ?

cd directory
for file in a b c
do
 sed 's/[0-9]\{1,\}/999/1' $file > $$
 mv $$ $file
done

---------- Post updated at 12:47 PM ---------- Previous update was at 12:45 PM ----------

This will replace a non-numeric string too...

1 Like

Hi Shamrock,

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

Hi ctsgnb,

First number of each row in each of the files

Thanks!

I've become so predictable. :slight_smile:

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.

printf '%s\n' ',s/^[^,]*/999/' w q | ed -s file

Regards,
Alister

alister, actually the first field in all the files is always a number.

Yup ! I was waiting for that one ! :smiley:

1 Like

Can you guys tell me how do I make the changes go into the files?

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

Got it with this..

awk -F",@," '{sub($1,999); print}' $file >> $file

Thanks guys, you rock !!!, I am amazed by the quick replies unlike the java forums I usually visit...

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.

Regards,
Alister

Yeah I realized that now. This seems to be taking care of it.

sed 's/[0-9]\{1,\}/999/1' $file > $$ 
mv $$ $file

The script you gave me

printf '%s\n' ',s/^[^,]*/999/' w q | ed -s $file 

is errroring out with new line found