Copying lines between two strings

Hello All,
I want to copy some lines from one file to other with following condition.

Only lines between two specified strings should copy.

Example :-

"My First String "
Some_Other_String ....
Some_Other_String ....
Some_Other_String ....
"My Second String"

So only lines between

 "My First String "

and

"My Second String"

should get copied to other ifle.

Try

awk '/\"My Second String\"/{a=""}
a{print}
/\"My First String \"/{a=1}' file > new_file
awk '/\"My Second String\"/{a=""}
a{print > "new_file"}
/\"My First String \"/{a=1}' file 

Or with Sed..

$ uname -rs
SunOS 5.10
$ sed -n '/First/{
> :l
> n
> /Second/{
> q
> }
> p; bl
> }' wer
Some_Other_String ....
Some_Other_String ....
Some_Other_String ....
$

Alternate awk..

nawk '/First/,/Second/{a=/First|Second/?0:1}a' inputfile

Dear michaelrozar17
Your solution works absolutely fine on command line but I have to use it in shell script.What are the changes required for that.Also I want little change in its behaviour , it should include both the strings in final output.Is it possible ?

It does not require any change to run in a shell script, just copy paste the sed or awk lines into a file and run. But remember to give full path name of the inputfile, its better coding.

$ cat print_patterns.ksh
#!/bin/ksh

echo "Printing between Patterns.."
sed '/First/,/Second/!d' /full/path/to/inputfile
$ chmod u+x print_patterns.ksh
$ ./print_patterns.ksh
Printing between Patterns..
"My First String "
Some_Other_String ....
Some_Other_String ....
Some_Other_String ....
"My Second String"
$
1 Like

Thank you michaelrozar17.
This solution is working fine.