0

I have one sql file that contain some data in this file some line as follow:

INSERT a=1 , b=2;  
INSERT a=2 , b=10;

like that so many lines are there i want to fetch these lines that contain "INSERT" keyword till semicolon and create a new file and save it into the new file.

using shell script.

2 Answers 2

1
$ sed -n '/^\s*INSERT/p' data > newfilename
$ cat newfilename 
INSERT a=1 , b=2;
INSERT a=2 , b=10;

or using grep:

$ grep '^INSERT' data > newfilename 
$ cat newfilename 
INSERT a=1 , b=2;
INSERT a=2 , b=10;

Using awk:

$ awk '/^INSERT/' data > newfilename
$ cat newfilename 
INSERT a=1 , b=2;
INSERT a=2 , b=10;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it is useful for me.
@anika welcome .. kindly click on the tick mark on the left side of the answer and get the question solved :)
0
cat test.sql 
insert a = 1; b= 2;
delete a, b;
insert d = 8;
insert g = 9;

grep 'insert' test.sql > newfile
cat newfile 
insert a = 1; b= 2;
insert d = 8;
insert g = 9;

This will also help you

^ (caret) means "the beginning of the line". So "^a" means find a line starting with an "a".

$ (dollar sign) means "the end of the line". So "a$" means find a line ending with an "a".

For example, this command searches the file myfile for lines starting with an "s" and ending with an "n", and prints them to the standard output (screen):

cat myfile | grep '^s.*n$'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.