textfile: copy between 2 keywords

Christian Gollwitzer auriocus at gmx.de
Thu Sep 10 13:29:08 EDT 2015


Am 10.09.15 um 13:18 schrieb Gerald:
> Hey,
>
> is there a easy way to copy the content between 2 unique keywords in a .txt file?
>
> example.txt
>
> 1, 2, 3, 4
> #keyword1
> 3, 4, 5, 6
> 2, 3, 4, 5
> #keyword2
> 4, 5, 6 ,7

If "copying" does mean copy it to another file, and you are not obliged 
to use Python, this is unmatched in awk:

Apfelkiste:Tests chris$ cat kw.txt
1, 2, 3, 4
#keyword1
3, 4, 5, 6
2, 3, 4, 5
#keyword2
4, 5, 6 ,7
Apfelkiste:Tests chris$ awk '/keyword1/,/keyword2/' kw.txt
#keyword1
3, 4, 5, 6
2, 3, 4, 5
#keyword2

Consequently, awk '/keyword1/,/keyword2/' kw.txt  > kw_copy.txt

would write it out to kw_copy.txt

Beware that between the two slashes there are regexps, so if you have 
metacharacters in your keywords, you need to quote them.

	Christian



More information about the Python-list mailing list