Re: Matching word and save text line
Sven Guckes <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
* mailbox20112002-SED <[email protected]> [2013-06-14 18:09]: > It possible with Sed, matching one word, > or more word in single line: YES and saving, > in other file the X lines previous, > and the Y following lines? > You have any example? generally possible in some way? yes. but - does it *have* to be sed? and why? out out funds for a good operating system? ;) because this really looks like a good example for the capabilities that "grep" already has. example1: $ X=3 Y=2 word=Prism file=/usr/share/dict/ngerman ofile1=results1.txt $ grep -B $X -A $Y $word $file > $ofile1 here are the relevant options for grep: -A, --after-context=NUM print NUM lines of trailing context -B, --before-context=NUM print NUM lines of leading context the rest comes from the evaluation of your shell, ie expansion of variables and redirection of output. you can also use grep with an extended set of regular expressions which allows alternative search patterns: example2: $ X=3 Y=2 word1=Prism word2=arnev file=/usr/share/dict/ngerman ofile2=results2.txt $ grep -E -B $X -A $Y "($word1|$word2)" $file > $ofile2 so.. if this is purely some academic homework.. maybe you want to share *your* answer with us? Sven ---------------- $ cat $ofile1 Prioritätsordnung Prioritätsrecht Prioritätssteuerung Prismen Pritschenwagen Privatadresse $ cat $ofile2 Karlspeises Karlsruhe Karlsruher Karneval Karnevale Karnevalen Karnevals Karnevalsmaske Karnickel Karnickeln -- Prioritätsordnung Prioritätsrecht Prioritätssteuerung Prismen Pritschenwagen Privatadresse