0

How to run simple grep command in a tcl script and get output

grep B file1 > temp  # bash grep command need to execute inside tcl commad,

file1 looks like this:

1 2 3 6 180.00 B
1 2 3 6 F 
2 3 6 23 50.00 B 
2 3 6 23 F

these do not work

exec grep B file.txt > temp
child process exited abnormally


exec "grep B pes_test.com > temp1"
couldn't execute "grep -e B ./pes_test.com > temp1": no such file or directory

exec /bin/sh -c {grep -e B ; true} < pes_test.com > tmp1
works but do not gives output, 
4
  • You will not get "child process exited abnormally" if the pattern you are looking for exists in the file. So it seems like you are just processing the wrong file; file.txt instead of file1. Commented Apr 7, 2021 at 18:33
  • 2
    Why drag an external program into it? It's trivial to open a file, and read lines and match a regular expression against each one in tcl. Commented Apr 7, 2021 at 19:09
  • okay thanks, @Shawn Commented Apr 7, 2021 at 19:17
  • 1
    You might also consider using the fileutil package from Tcllib. It has a grep command to do the heavy lifting. Commented Apr 8, 2021 at 1:24

1 Answer 1

1

exec throws an error when the process returns non-zero. See exec and the Tcl wiki

try {
    set result [exec grep $pattern $file]
} on error {e} {
    # typically, pattern not found
    set result ""
}

Ref: try man page

Sign up to request clarification or add additional context in comments.

1 Comment

It's very irritating that the CHILDSTATUS error code puts the process ID before the exit code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.