bash - Redirecting output of list of commands -
i using grep pull out lines match 0.
in multiple files. files not contain 0.
, want output "none" file. if finds matches want output matches file. have 2 example files this:
$ cat sea.txt shrimp 0.352 clam 0.632 $ cat land.txt dog 0 cat 0
in example files both lines output sea.txt
, land.txt
file "none" using following code:
$ grep "0." sea.txt || echo "none"
the double pipe (||
) can read "do foo or else bar", or "if not foo bar". works perfect problem having cannot output either matches (as find in sea.txt
file) or "none" (as find in land.txt
file) file. prints terminal. have tried following others without luck of getting output saved file:
grep "0." sea.txt || echo "none" > output.txt grep "0." sea.txt || echo "none" 2> output.txt
is there way save file? if not there anyway can use lines printed terminal?
you can group commands { }
:
$ { grep '0\.' sea.txt || echo "none"; } > output.txt $ cat output.txt shrimp 0.352 clam 0.632
notice ;
, mandatory before closing brace.
also, i've changed regex 0\.
because want match literal .
, , not character (which unescaped .
does). finally, i've replaced quotes single quotes – has no effect here, prevents surprises special characters in longer regexes.
Comments
Post a Comment