How do I search first&second string & copy all content between them to other file?

Hi All,

How do I search first string & second string and copy all content between them from one file to another file?

Please help me..

Thanks In Advance.

Regards,
Pankaj

Are you trying to ask?

file1=

blah
blah
ABC
123
456
789
DEF
yech
yech

Thus, search file1 to copy from ABC to DEF to file2
ABC
123
456
789
DEF

Yes. exactly
this is my result what u have got in second file

Please help me to get that.

I would guess that there is a simpler solution with awk, but the following code uses sed, tr and grep -- three of my favorites -- to solve this.

> cat file73
blah
blah
ABC
123
456
789
DEF
yech
yech


> sed "s/^ABC/#ABC/" <file73 | sed "s/DEF$/DEF#/" | tr "\n" "~" | tr "#" "\n" | grep "^ABC" | tr "~" "\n"
ABC
123
456
789
DEF

Hi,

wouldn't

sed -n '/^ABC/,/^DEF/p' file

be simpler? This tells sed to output only the lines between lines starting with ABC and lines starting with DEF.

HTH Chris

Hi All,

Thanks for helping me but I am trying to search date. I mean my first string is like [11/10/08 8:43:58:610 EST] and second is [11/10/08 8:44:44:340 EST] and I want data between these tow lines(OR Strings).

I have tried your commands for the strings its working fine but please help me in above case.

THANKS

Have you tried the sed command above? It should be something like:

sed -n '/[11/10/08 8:43:58:610 EST]/,/[11/10/08 8:44:44:340 EST]/p' file > newfile

Regards

Hi,

I have tried that command but in result i.e. in newfile I am getting all content which are in file. I am getting newfile as it is as file.

Post your input file and the desired output.

Regards

File1
[11/10/08 8:43:43:960 EST] 00000107 sdfsdfsdfsdfsdf
[11/10/08 8:43:43:961 EST] 00000107 sdfsdfsdfsdfsdfsdf
[11/10/08 8:43:43:961 EST] 00000107 sdfsdfsdfsdfsdfsdfsdfsdf
[11/10/08 8:43:43:961 EST] 00000107 sdfsdfsdfsdfsdfsdfsdf
[11/10/08 8:43:43:961 EST] 00000107 sdfsdfsdfsdfsdfsdfsdsdf
[11/10/08 8:43:58:570 EST] 0000009a sdfsdfsdfsdfsdfsdfsdfsdfsdf
[11/10/08 8:43:58:570 EST] 0000009a sdfsdfsdfsdfsdsdfsdf
[11/10/08 8:43:58:570 EST] 0000009a sdfsdfsdffffffff
[11/10/08 8:43:58:609 EST] 0000009a sdffffffffff
[11/10/08 8:43:58:610 EST] 0000009a sdfffffwetwert
[11/10/08 8:44:44:340 EST] 0000009a sdfgsdfsdfsdfsd

File2 (desired output)
[11/10/08 8:43:58:610 EST] 0000009a sdfffffwetwert
[11/10/08 8:44:44:340 EST] 0000009a sdfgsdfsdfsdfsd

Here my first string is [11/10/08 8:43:58:610 EST] and second string is [11/10/08 8:44:44:340 EST]

Sorry, I forgot to escape the square brackets and the slashes:

sed -n '/\[11\/10\/08 8:43:58:610 EST\]/,/\[11\/10\/08 8:44:44:340 EST\]/p'

Hi All,

Thanks for your help.

Its working...

Thnaks Franklin52

Regards,

Pankaj

Thanks guys!

I was needing to do this very thing to extract the RSA Key from a converted pfx file but I forgot the syntax in sed.

sed -n '/^-----BEGIN RSA/,/^END RSA PRIVATE KEY-----/p' file1 > file2

Having a good title makes it easy to find what you're looking for!