I want to display all of the lines in the file inventory that contain the word Straw right before a space.

The data file inventory contains:

Strawberry Jam,300,4
Raspberry Jam,1216,7
Blueberry Jam,96,195
Strawberry Compote,49,621
Raspberry Compote,1937,624
Blueberry compote,200,625
Frozen Strawberries,130,1941
Straw Hats,16,2047

I have tried grep ".Straw" inventory.

  • Could you tell us what you mean by "not working"? Is there an error we should know of? A pattern that's missed? – SYN Nov 19 '16 at 20:19
  • its an assignment that i have to do and it doesnt consider it as valid answer – Lovya Bird Hasse801 Nov 19 '16 at 20:23
  • Is your sample inventory formatting consistent (all in one line)? Reading unix.stackexchange.com/questions/324566/…, it is still unclear: do you need your space before "Straw", or after? – SYN Nov 19 '16 at 20:43
  • No they are in all different lines – Lovya Bird Hasse801 Nov 19 '16 at 20:44
  • What research have you done? In particular, I’m curious what gave you the idea that putting a . before Straw would enable you to match a space after the word Straw. Also, please explain: Are you trying this on an actual system, where success or failure is determined by whether or not a command gives the correct answer?  Or are you trying this on a test system, where you type in a command, and it says “Yes” if you (exactly?) match the command it wants you to give, and “No” if you enter anything else (even if it would actually work on a real system)? – Scott Nov 19 '16 at 23:01

Maybe with something like this:

grep -iE '(^|[^a-z])straw ' inventory

Lines starting with "straw ", and line matching a non-alphabetical character followed by "straw " (may need to match out numbers as well, ... depends on your inventory file).

Update: if you need your space to be before the word straw, then:

grep -i ' straw' inventory
grep -iE ' straw( |$)' inventory
  • nah it didn't work – Lovya Bird Hasse801 Nov 19 '16 at 20:34
  • 1
    @LovyaBirdHasse801: Please stop saying “nah it didn’t work”. Please edit your question with (1) the output of the command od -ab inventory, (2) the output that you want, (3) the output that you’re getting from the command you tried, and (4) the output that you’re getting from each command that’s suggested in an answer. This one in particular, as it looks to me like it should work. – Scott Nov 19 '16 at 23:00

Your command line seems to be ok to me, maybe there's not a space after "Straw".

Example: I created a file with a line with space and another with tab right after "Straw".

$ cat inventory
00 Strawberry
12Straw space
21Straw tab
cba Straw

Let's execute your command:

$ grep -i 'straw ' inventory
12Straw space

Now let's execute the same, but with character classes instead:

$ grep -i 'straw[[:space:]]' inventory
12Straw space
21Straw tab

Pipe cat to grep? cat inventory | grep "^straw\ "

Could also be achieved with sed and printf


basicly I'd go with the others and say

cat inventory | grep straw

as for me this should be the solution. Even if it might be a 'cat abuse'.


I had the same problem, so I have decided to share it with others. All of them are correct.

  1. egrep "Straw[[:space:]]" inventory
  2. grep "Straw[ ]" inventory
  3. grep "Straw " inventory

But only the last one works for that Linux assignment.

  • for 3rd command after Straw , there is space then use " – Ali Jul 22 '17 at 3:46

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.