TransWikia.com

How do I run grep on a zsh variable?

Unix & Linux Asked by Serge Vu on November 28, 2020

I’d like to inline the following 2 commands:

big_query_that_returns_text > in.txt
$ printf '%sn' "foo" "bar" | grep -f /dev/stdin in.txt

that do work by finding foo and bar in in.txt

but when I try to

printf '%sn' "foo" "bar" | grep -f /dev/stdin big_query_that_returns_text

I receive

zsh: argument list too long: grep

I also tried

var=`big_query_that_returns_text`
printf '%sn' "foo" "bar" | grep -f /dev/stdin $var
printf '%sn' "foo" "bar" | grep -f /dev/stdin "$var"

but I receive the same error.

2 Answers

This is a place for a process substitution: it's a block of code that acts like a file

Pipe the big query results to grep's stdin

big_query_that_returns_text | grep -f <(printf '%sn' "foo" "bar")

If the command to produce "foo" and "bar" is more complicated, you can help readability with arbitrary newlines inside the process substitution:

big_query_that_returns_text 
| grep -f <(
      printf '%sn' "foo" "bar"
  )

Correct answer by glenn jackman on November 28, 2020

It seems you want to search for foo or bar in a zsh variable that basically is the output of some command. I'm not sure why you go the extra way to pipe the search strings to grep, but unless this is absolutely required, you could do

big_query_that_returns_text | grep -E "foo|bar"

If you want to use the output of big_query_that_returns_text in more than one place, and for this reason store it in a shell variable, you can still use

var=$(big_query_that_returns_text)

printf '%sn' "$var" | grep -E "foo|bar"

... (other operations on $var) ...

Here using printf '%sn'. Other alternatives would be print -r -- "$var" or echo -E - "$var" or use a here-document or here-string. All of printf, print and echo are built-in in zsh, so you won't get the argument list too long error which is a limitation of the execve() system call, so only applies to external commands that are executed (like grep which is not builtin in zsh).

Answered by AdminBee on November 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP