Extracting sender's number using arduino

Hi!
I have tried extracting the number of the sender using the code below. My approach is to save +CMT string in a char array and then extract the number using particular index. But my first step is not working, it doesn't save the +CMT string like I expect it to.
Please help.

char recMsg[20];

void setup() 
{

  Serial.begin(9600);
  Serial1.begin(9600);
  
  Serial.write("AT+CMGF=1\r");           //set GSM to text mode
  delay(1500);
  Serial.write("AT+CNMI=2,2,0,0,0");      //Preferred SMS Message Storage

  Serial.print("Ready");
}

void loop() 
{
  while(true)
  {
  if(Serial1.available()>0)
  {
    for (int i=0; i<20; i++)
    {
      recMsg[i] = Serial1.read();
      Serial.print("AT+CMGD=4"); // delete msgs
    }
  }

  for(int j=0; j<20; j++)
  {
    Serial.print(recMsg[j]);
  } 
  }
}

Classic trap. You check for at least one byte to be available, but then you try to read twenty bytes. Serial data is slow. You're reading it faster than it arrives. Change your available line to check if Serial.available() >= 20.

  Serial.write("AT+CMGF=1\r");           //set GSM to text mode

Is the GSM attached to Serial or Serial1 ?

It is attached to Serial1.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

Dove5:
It is attached to Serial1.

So, is
Serial.write("AT+CMGF=1\r");          //set GSM to text modegoing to do what the comment says ?

Serial.write seems to work the same way as Serial1.write. But I have changed it in my code now.

Serial.write seems to work the same way as Serial1.write.

It works the same except that ot uses different Tx and Rx pins

I have changed it in my code now.

Is it working now ?

Yes, it works.