I am facing an issue of searching through lines of text in a text file.Currently, I am using this commandcheck=`awk -F : -v title=$title 'tolower($1) ~ tolower(title)' test.txt`It works fine when the strings are pure alphabetic characters. Assuming the text file contains 3 lines of string which areC++ Programming in 21 DaysC## Programming in 21 DaysC Programming in 21 DaysWhen I do a partial search for just a letter C, 3 of the results will be displayed, which is what I want, however, if I keyed in C++ P, my program will return text not found. And if i key in C++, all 3 results will be displayed as well.But the funny thing is, if I search for C## P, my program will return C## Programming in 21 Days found.I can't seem to find out what is causing this error, please help.
Usage of awk to search for string that contains characters
awk
The + is getting treated as a regular expression.  $ title=C++ P$ awk -F: -v title=$title 'tolower($1) ~ tolower(title)' test.txtC Programming in 21 Days$ title=C.. P$ awk -F: -v title=$title 'tolower($1) ~ tolower(title)' test.txtC++ Programming in 21 DaysC## Programming in 21 DaysIf you're only interested in matching the start, you could use$ awk -F: -v title=$title 'tolower(substr($0,0,length(title))) == tolower(title)' test.txtOr to match anywhere within the line$ title=C$ awk -F: -v title=$title 'index(tolower($0),tolower(title))' test.txtC++ Programming in 21 DaysC## Programming in 21 DaysC Programming in 21 Days$ title=C++ P$ awk -F: -v title=$title 'index(tolower($0),tolower(title))' test.txtC++ Programming in 21 Days$ title=C## P$ awk -F: -v title=$title 'index(tolower($0),tolower(title))' test.txtC## Programming in 21 Days