Displaying all the lines starting with some keyword

consider the contents of a file has many stuff including few stuff that i need.. so i perfromed the below function

cat filename | grep "ALTER TABLE"

its output is as shown below

.
.
.
.
.        SET @sql:=CONCAT('ALTER TABLE RecordMixProfile AUTO_INCREMENT=', @maxId) ;
        SET @sql:=CONCAT('ALTER TABLE AccountPolicy AUTO_INCREMENT=', @maxId) ;
        SET @sql:=CONCAT('ALTER TABLE Enterprise AUTO_INCREMENT=', AUTO_INCREMENT=', @maxId) ;
        SET @sql:=CONCAT('ALTER TABLE UserGroup AUTO_INCREMENT=', @maxId) ;
ALTER TABLE Trunk ALTER mediaServerProfileId SET DEFAULT 2;
ALTER TABLE DeviceUDA ALTER mediaServerProfileId SET DEFAULT 2;
ALTER TABLE Trunk ADD INDEX idx_Trunk(destinationAddress);
ALTER TABLE ButtonResourceAppearance ADD INDEX idx_parentButtonId

but i need only the statements that starts with ALTER TABLE like the last 4 lines... how to filter the output..? i am not good at awk nor sed...

Try

grep "^ALTER TABLE" filename
1 Like

thanks a lot it worked :-).. but what does '^' do in the code...?

^ refers to start of input string. It will fetch only those lines that have ALTER TABLE at the beginning of line.

1 Like

oh okay thanks... wait what if the input contents are like this..

.
.
        SET @sql:=CONCAT('ALTER TABLE Enterprise AUTO_INCREMENT=', AUTO_INCREMENT=', @maxId) ;
        SET @sql:=CONCAT('ALTER TABLE UserGroup AUTO_INCREMENT=', @maxId) ;
                  ALTER TABLE Trunk ALTER mediaServerProfileId SET DEFAULT 2;
                  ALTER TABLE DeviceUDA ALTER mediaServerProfileId SET DEFAULT 2;
 

that is few empty space before start of ALTER TABLE... will the command work still?
i checked just now it wont work if there is empty space before ALTER TABLE... how to filter if this situation arises??

grep "^ *ALTER TABLE" inputfile

its not printing all the lines...

cat tble
        ALTER TABLE Trunk ALTER mediaServerProfileId SET DEFAULT 2;
        ALTER TABLE DeviceUDA ALTER mediaServerProfileId SET DEFAULT 2;
    ALTER TABLE DeviceTurret ALTER mediaServerProfileId SET DEFAULT 1;
                ALTER TABLE Trunk ADD INDEX idx_Trunk(destinationAddress);
 ALTER TABLE ButtonResourceAppearance ADD INDEX idx_parentButtonId(parentButtonId);
                        ALTER TABLE TrunkAOR ADD INDEX idx_parentTrunkId(parentTrunkId);
      ALTER TABLE Button ADD INDEX idx_parentUserCDIId(parentUserCDIId);
   ALTER TABLE CertificateInfo ADD INDEX idx_fingerprint(fingerprint);
          ALTER TABLE SoftwarePackage ADD CONSTRAINT idx_name_parentZoneId UNIQUE (name,parentZoneId);
ALTER TABLE Component ADD CONSTRAINT idx_name_parentSoftwarePackageId UNIQUE (name,parentSoftwarePackageId);
[root@dunkin-ds-dev-103 vivek]#
[root@dunkin-ds-dev-103 vivek]#
[root@dunkin-ds-dev-103 vivek]# grep "^ *ALTER TABLE" "tble"
    ALTER TABLE DeviceTurret ALTER mediaServerProfileId SET DEFAULT 1;
 ALTER TABLE ButtonResourceAppearance ADD INDEX idx_parentButtonId(parentButtonId);
      ALTER TABLE Button ADD INDEX idx_parentUserCDIId(parentUserCDIId);
   ALTER TABLE CertificateInfo ADD INDEX idx_fingerprint(fingerprint);
          ALTER TABLE SoftwarePackage ADD CONSTRAINT idx_name_parentZoneId UNIQUE (name,parentZoneId);
ALTER TABLE Component ADD CONSTRAINT idx_name_parentSoftwarePackageId UNIQUE (name,parentSoftwarePackageId);

u can use sed and grep combination,

sed 's/^[ \t]*//g;' filename | grep "^ALTER TABLE"

This will remove all tabs and spaces at starting and then match...

1 Like

thanks.. the code is working good :slight_smile:

@vivek_d_r: Grep command in post #6 works for me. Please check again.

[root@hostname test]# cat input
                ALTER TABLE Trunk ALTER mediaServerProfileId SET DEFAULT 2;
        ALTER TABLE DeviceUDA ALTER mediaServerProfileId SET DEFAULT 2;
    ALTER TABLE DeviceTurret ALTER mediaServerProfileId SET DEFAULT 1;
                ALTER TABLE Trunk ADD INDEX idx_Trunk(destinationAddress);
 ALTER TABLE ButtonResourceAppearance ADD INDEX idx_parentButtonId(parentButtonId);
                        ALTER TABLE TrunkAOR ADD INDEX idx_parentTrunkId(parentTrunkId);
      ALTER TABLE Button ADD INDEX idx_parentUserCDIId(parentUserCDIId);
   ALTER TABLE CertificateInfo ADD INDEX idx_fingerprint(fingerprint);
          ALTER TABLE SoftwarePackage ADD CONSTRAINT idx_name_parentZoneId UNIQUE (name,parentZoneId);
ALTER TABLE Component ADD CONSTRAINT idx_name_parentSoftwarePackageId UNIQUE (name,parentSoftwarePackageId);

[root@hostname test]# grep "^ *ALTER" input
        ALTER TABLE Trunk ALTER mediaServerProfileId SET DEFAULT 2;
        ALTER TABLE DeviceUDA ALTER mediaServerProfileId SET DEFAULT 2;
    ALTER TABLE DeviceTurret ALTER mediaServerProfileId SET DEFAULT 1;
                ALTER TABLE Trunk ADD INDEX idx_Trunk(destinationAddress);
 ALTER TABLE ButtonResourceAppearance ADD INDEX idx_parentButtonId(parentButtonId);
                        ALTER TABLE TrunkAOR ADD INDEX idx_parentTrunkId(parentTrunkId);
      ALTER TABLE Button ADD INDEX idx_parentUserCDIId(parentUserCDIId);
   ALTER TABLE CertificateInfo ADD INDEX idx_fingerprint(fingerprint);
          ALTER TABLE SoftwarePackage ADD CONSTRAINT idx_name_parentZoneId UNIQUE (name,parentZoneId);
ALTER TABLE Component ADD CONSTRAINT idx_name_parentSoftwarePackageId UNIQUE (name,parentSoftwarePackageId);

hi balajesuri,

grep "^ *ALTER TABLE" inputfile

The above command will only search only for lines which have 0 to n spaces followed by the PATTERN and neglect if the PATTERN is after tab space(\t)

Check with tab at the starting...

1 Like

oh yeah i have used tab infront of few of the lines... so thats the reason it dint work

Ah, that's right! I didn't think of that.

grep "^[ \t]*ALTER" input

Here's another alternative:

perl -ne '/^\s*ALTER/ && print' input

Hi balajesuri,

Did u tried this command

grep "^[ \t]*ALTER" input

as when i tried it in my Linux machine its not giving the lines with tab...

Thanks a lot for helping me out ... :slight_smile: