Ubuntu – Search specific text from file and output the line (Shell script)

bashcommand linescripts

Suppose I have a variable $str1

str1="20140121-21:48:26|Joanne Frances|"

What I want to do is search that part in the file. The file contains, for example:

20140121-21:48:26|Joanne Frances|Perez|9173046751.000000|Mar 31|
20140121-21:50:28|Nikko|Real|9065887272.000000|Mar 21|
20140121-22:22:53|Czarina|Revilla|9999999999.000000|Jan 21|

The output should be:

20140121-21:48:26|Joanne Frances|Perez|9173046751.000000|Mar 31|

Update: Finally know what the heck is the problem tried to echo $str1 and it outputs 20140121|Adrian Perez| meaning it really ISN'T in the file. Haaa! I'm such a mess! -_-

Best Answer

Use this:

grep "$str" filename.txt

to prevent misinterpretation of special characters.

To know more, refer this: