Issue
I have a text file that looks something like this...
<title> south asia </title>
India is country that is part of south asia.
<title> africa </title>
kenya is a country that is part of africa.
This command works as expected and returns the correct category...
grep -B1 'kenya' wiki.txt | grep title
But this trick will not work if the text file looks something like this...
<title> south asia </title>
India is country that is part of south asia.
<title> africa </title>
List of countries:
kenya is a country that is part of africa.
If I do not know the correct value for "Before" parameter, then I get extra (wrong) titles.
# grep -B5 'kenya' wiki.txt | grep title
<title> south asia </title>
<title> africa </title>
Is it possible to select the last "title" per group while using -B parameter?
Expected: The line title africa title should be returned for the word "kenya" even if I do not know the number of lines used in the article.
Solution
With awk
simply store the title
records in a variable (t
) and print them when you encounter the matching word (variable w
):
$ awk -vw='kenya' '/<title>/ {t=$0} $0~w {print t}' wiki.txt
<title> africa </title>
Answered By - Renaud Pacalet Answer Checked By - Gilberto Lyons (WPSolving Admin)