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! -_-

  • you can simply use grep. e.g. grep $str filename.txt – mcantsin Jan 21 '14 at 18:04
  • @mcantsin I tried that, it didn't work – joanne_ Jan 21 '14 at 18:06
  • and what's the error? - No output? – mcantsin Jan 21 '14 at 18:08
  • @mcantsin yup no output! – joanne_ Jan 21 '14 at 18:10
  • I tested your example and it works perfectly for me. - Sure there is no typo? – mcantsin Jan 21 '14 at 18:12

Use this:

grep "$str" filename.txt

to prevent misinterpretation of special characters.

To know more, refer this:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.