Discussion:
Matching word and save text line
mailbox20112002-SED
2013-06-14 15:48:07 UTC
Permalink
Hi all

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?


Thanks

Regards
Sven Guckes
2013-06-14 16:28:24 UTC
Permalink
Post by mailbox20112002-SED
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
Sven Guckes
2013-06-14 22:20:15 UTC
Permalink
my o.s. is Windows Xp
kewl
Grep work better than sed for this problem?
they both work. take a look at my answer and try it.
then take a look at the answers about sed solutions.
compare, think, decide - this part is on you. and
please dont top post. or at least edit. thankyou.

Sven

Loading...