Home > database >  Extract only server version from an SQL file
Extract only server version from an SQL file

Time:02-02

I am wanting to grep an SQL file to get the following information, however, I am struggling to get it to work - I've tried the following;

grep -Poe "-- Server version \K.*" _schemas.sql

The line in the SQL dump is

    -- MySQL dump 10.13  Distrib 5.1.55, for unknown-linux-gnu (x86_64)
--
-- Host: 10.213.118.88    Database: mysql
-- ------------------------------------------------------
-- Server version   5.1.55

The format will not change - I only want to grep the actual numbers of the server version, so in this case, 5.1

sqlver=(awk '/Server version/ { split($NF,a,"."); print a[1] "." a[2] }' _schemas.sql)

if [ $sqlver -eq 5.1 ];

then

/opt/migrate/db_import.sh: line 118: [: awk: integer expression expected

CodePudding user response:

To strip the leftmost spaces, add "*" before \K

grep -Poe "-- Server version *\K.*" _schemas.sql  #5.1.55

To retain only 5.1 in your case, replace \K.* with *\K.{3} (What You Want)

grep -Poe "-- Server version *\K.{3}" _schemas.sql  #5.1

Where You are not sure whether it's tabs or spaces, use:

grep -Poe "-- Server version[ \t]*\K.{3}" _schemas.sql  #5.1

Corresponding sed Versions:

sed -nE "s/-- Server version *(.*)/\1/p" _schemas.sql      #5.1.55

sed -nE "s/-- Server version *(.*)\..*/\1/p" _schemas.sql  #5.1
sed -nE "s/-- Server version[ \t]*(.*)\..*/\1/p" _schemas.sql #5.1

CodePudding user response:

One awk idea:

$ awk '/Server version/ { print $NF }' _schemas.sql
5.1.55

If the intent is to display just the first 2 decimal-delimited numbers:

$ awk '/Server version/ { split($NF,a,"."); print a[1] "." a[2] }' _schemas.sql
5.1

CodePudding user response:

The following gives you the first two components of the version.

-- Server version and the version string can be separated by any number of spaces or tabs.

sed -ne "s/^-- Server version[ \t]*\([0-9]\{1,\}\.[0-9]\{1,\}\)\..*$/\1/p" < _schemas.sql
  • Related