I'm trying to use PCRE2 on stdin in a bash script but I'm not sure it's possible based on reading the docs. I know I can give it the filename but I'm doing a test and it specifically says on stdin.
For example I have this text in input.txt
foo blah is a bar
this shouldn't work
I then call this command: cat input.txt | sh program.sh "foo %{0} is a %{1S3}"
Which fires this shell script:
#!/bin/bash
# Give access to Elixir module
chmod x ./lib/my_mod.ex
# Get arg passed to program.sh
token=$1
# Create array for cat and insert into it
array=()
while IFS= read line; do
array =("${line}")
done
# Get regex based on pattern sequence
regex=$(mix run -e "my_mod.main('${token}')")
pcre "${regex}" ./input.txt
# Call pcre2grep on each item and match with regex
# for value in "${array[@]}"
# do
# echo "${value}"
# pcre2grep "${regex}" "${value}"
# done
This works: pcre "${regex}" ./input.txt
This doesn't
# Call pcre2grep on each item and match with regex
for value in "${array[@]}"
do
echo "${value}"
pcre2grep "${regex}" "${value}"
done
I'm supposed to use PCRE (Or supposedly PCRE2) but I feel like I'm doing something stupid.
Any ideas on if this is possible using PCRE?
Thanks Nick
Update
If I hard code the regex returned from $(mix run -e "my_mod.main('${token}')")
it doesn't work but if I hardcode the regex, it does work:
pcre2grep "foo [a-zA-Z ]{0,} is a \W*(\w (\W |$)){1,4}$"
Calling Mix is affecting cat input.txt | sh program.sh "foo %{0} is a %{1S3}"
CodePudding user response:
Just run it, just like grep:
regex=$(mix run -e "my_mod.main('${token}')" <&-)
pcre2grep "${regex}"
It happens that mix
is reading from standard input, leaving nothing for pcre2grep
to read. Close standard input for the process with <&-
.