Search code examples
bashshellscriptingdash-shellash

How to use tab separators with grep in ash or dash script?


Task at hand:

I have a file with four tab separated values:

peter 123   five   apples
jane  1234  four   rubberducks
jimmy 01234 seven  nicknames

I need to get a line out of this file based on second column, and the value is in a variable. Let's assume I have number 123 stored in a variable foo. In bash I can do

grep $'\s'$foo$'\s'

and I get out of peter's info and nothing else. Is there a way to achieve the same on dash or ash?


Solution

  • You can use awk here:

    var='1234'
    awk -v var="$var" '$2 == var ""' f
    jane  1234  four   rubberducks
    

    PS: I am doing var "" to make sure var is treated as a string instead of as a number.