Please help. i’m struggling with a line printing one word before and one word after the match. ideally is to make number of words variable, but at lease 1 is needed.
sample Input
https://suttacentral.net/sn45.78-82 1 Saṁyutta Nikāya 45.78–82 8. Dutiyaekadhammapeyyālavagga Sīlasampadādisuttapañcaka
sn45.78-82 yathayidaṁ, bīṁāṅñhikkhave, chandasampadā …pe…
https://suttacentral.net/sn45.8 4 Saṁyutta Nikāya 45.8 1. Avijjāvagga Vibhaṅgasutta
sn45.8 Idha, bhikkhave, bhikkhu anuppannānaṁ pāpakānaṁ akusalānaṁ dhammānaṁ īṁāṅñanuppādāya chandaṁ janetīṁāṅñi vāyamati vīriyaṁ ārabhati cittaṁ paggaṇhāti padahati,
expected Output
bīṁāṅñhikkhave, chandasampadā …pe…
īṁāṅñanuppādāya chandaṁ janetīṁāṅñi
i don’t know how to deal with symbols like **ī ṁ ā ṅ ñ ** etc
word related regexs don’t handle these symbols properly
what i use
pattern=chand
grep -oP '(?:s*D?s*){0,'10'}'"$pattern"'(?:s*D?s*){0,'10'}'
what i get
ve, chandasampadā …pe…
▒ya chandaṁ janeti
please advice some solution. Grep, sed, awk, whatever available on default centos (can’t install other utils)
2
Answers
Assumptions:
Adding the following lines to the end of OP’s input file:
One
awk
idea based on a hard-coded before/after count of1
:This generates:
Expanding to handle a user-defined count of leading/trailing words:
For
cnt1=1 / cnt2=1
this generates:For
cnt1=2 / cnt2=2
this generates:For
cnt1=1 / cnt2=2
this generates:Assuming that, as in the example you provided, the target word only appears once per input line or is separated by at least 2*num words from other occurrences of it:
The above uses GNU grep for
-o
ands/S
and assumes you want to do regexp matching as you’re doing in the question rather than string matching.