I have a file similar to:abcdef123ghiflk234sfhskdhf483I would like to search for a string, and the output should include the next (and/or previous) line as well.So for example:grep bcd myfileOutput:abcdef123Or show previous line:grep ifl myfileOutput:123ghiflkI tried for days now to find a solution online but can't seem to figure it out.
How can I grep a certain text and display its line and the line after as well
grep
The GNU and BSD grep utilities has the a -A option for lines after a match and a -B option for lines before a match.  Thus, you can do something like:$ grep -A 1 bcd myfileabcdef123to show the line after the match and $ grep -B 1 ifl myfile123ghiflkto show the line preceding the match. Finally, you can use -C to show N lines before and after the match:$ grep -C 1 ifl myfile123ghiflk234If your version of grep doesn't support this feature, you could always try some other methods:Using POSIX grep and sed options along with shell:n=$(grep -n bcd foo | cut -d':' -f1); sed -n ${n},$((n + 1))p myfileUsing AWKawk '/bcd/ {print; getline; print}' myfileAnd many many more.