SED/AWK file read & manipulation

I have large number of data files, close to 300 files, lets say all files are same kind and have extension .dat , each file have mulitple lines in it.

There is a unique line in each file containing string 'SERVER'. Right after this line there is another line which contain a string 'DIGIT=0', however this line 'DIGIT=0' appears at other places within the file. I need to know how I can change this 'DIGIT=0' to 'DIGIT=1' only where it appears after the line containing the string 'SERVER' and not at other places in the file. All the lines needs to be redirected to another file if the change is not possible on the original file.

Example of cat mydata.dat file:

APP1
APP3
DIGIT=2
DIGIT=1
DIGIT=0
DIGIT=0
APP3
APP2
SERVER
DIGIT=0 <----- I need to change this DIGIT=0 to DIGIT=1
DIGIT=1
APP2

----------------------------------------------------------
If I use sed then it changes DIGIT=0 at other places
for example: sed -e 's/DIGIT=0/DIGIT=1/' file > newfile

I am not that shell script guru so need help from you all, I think it should be simple but I will learn some thing new.

Thanks a lot.

Hi, sal_tx:

sed '/SERVER/{n;/^DIGIT=0/s/0/1/;}' file

Regards,
Alister

awk '/SERVER/ {print;getline;gsub(/DIGIT=0/,"DIGIT=1",$0)}1' urfile

Try:

perl -i  -lne '$f=1 if (/SERVER/);  if ($f && (/DIGIT=0/)) {print "DIGIT=1"; $f=0; } else { print };'  *.dat

Thanks all, I just received the code from Allister, tried the sed command and it worked.

I will also try awk script & perl also, these will be handy scripts.

My best regards.