Displaying all the lines starting with some keyword


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Displaying all the lines starting with some keyword
# 1  
Old 12-29-2011
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

Code:
cat filename | grep "ALTER TABLE"

its output is as shown below
Code:
.
.
.
.
.        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...
# 2  
Old 12-29-2011
Try

Code:
grep "^ALTER TABLE" filename

This User Gave Thanks to siva shankar For This Post:
# 3  
Old 12-29-2011
thanks a lot it worked :-).. but what does '^' do in the code...?
# 4  
Old 12-29-2011
^ refers to start of input string. It will fetch only those lines that have ALTER TABLE at the beginning of line.
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 12-29-2011
oh okay thanks... wait what if the input contents are like this..
Code:
.
.
        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??
# 6  
Old 12-29-2011
Code:
grep "^ *ALTER TABLE" inputfile

# 7  
Old 12-29-2011
its not printing all the lines...
Code:
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);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk join lines based on keyword

Hello , I will need your help once again. I have the following file: cat file02.txt PATTERN XXX.YYY.ZZZ. 500 ROW01 aaa. 300 XS 14 ROW 45 29 AS XD.FD. PATTERN 500 ZZYN002 ROW gdf gsste ALT 267 fhhfe.ddgdg. PATTERN ERE.MAY. 280 PATTERRNTH 5000 rt.rt. ROW SO a 678 PATTERN... (2 Replies)
Discussion started by: alex2005
2 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

4. Shell Programming and Scripting

Extract Lines Containg a Keyword

Hi , I have two files, say KEY_FILE and the MAIN_FILE. I am trying to read the KEY_FILE which has only one column and look for this column data in the MAIN_FILE to extract all the rows that have this key. I have written a script to do so, but somehow it is not returning all the rows ( It... (4 Replies)
Discussion started by: Sheel
4 Replies

5. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

6. Shell Programming and Scripting

Delete the lines before the first instance of the keyword

I have my data something like this. I want to delete all the lines before the frist instance of the key word 'ravi kumar' aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. 1234 ravi kumar aaaaaa... (8 Replies)
Discussion started by: rdhanek
8 Replies

7. Shell Programming and Scripting

Delete the lines after the last instance of the keyword

I have my input sometyhing like this aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar rrrrrrr mmmmmmm I want the output as follows. aaa bbbbbb cccccc ddddd eeeee 1234 ravi kumar aaaaaa vvvvvvv 5678 ravi kumar (2 Replies)
Discussion started by: rdhanek
2 Replies

8. Shell Programming and Scripting

How can i delete a keyword starting with x in unix

I am trying to delete key word starting with x in a unix text file. example, I am trying to delete the words like xaa,xabxbb,xbd and so on.... my input file is some thing like this xaaa w 1234 5678 rwsd ravi xw123 xbc3 ohrd want to delete words xaaa,xw123 and xbc3 from the above... (10 Replies)
Discussion started by: rdhanek
10 Replies

9. Shell Programming and Scripting

Cut lines before keyword

Hi, How to cut all lines in file before keyword? from 1 2333214 word ...... some text 2 234343 234234 word ...... some text 3 234324 324 3234 word ...... some text to 1 2333214 2 234343 234234 3 234324 324 3234 (4 Replies)
Discussion started by: Trump
4 Replies

10. Shell Programming and Scripting

display (ehco) 5 lines before and after find keyword

I like to display 5 lines before and after find some keywork (i.e. error). Thanks in advance (5 Replies)
Discussion started by: wangzosen
5 Replies
Login or Register to Ask a Question