f



Problem using grep command in tcl and reading multiple files

Hi All,

I am a newbie to tcl and i am having problem performing some of basic
operations using tcl.

I am having a centralized location in server where i am large number
of directories with all user logins and each directory is having some
log files with *.log extension. My requirement is to identify
directories with my login id and then go into each directory and
append all the information available in *.log files into one file.

Problem 1:

For identifying directory name with my login, i am using

exec ls | grep <USERNAME>

But it is returning "child process exited abnormally"

If i run exec ls only, it lists down all the directories.

What is wrong with "exec ls | grep <USERNAME>"

Other related question, in tcl is there any inbuilt command that can
be used to grep directories with a particular login like file command?


Problem2:

Other question is regarding opening multiple files and dumping in one
file.

I am trying following code

set file_handle [open "*.log" "r"]

But is giving error.

So i think i should do it like

set list_dir [glob *.log]
foreach file_name $list_dir {
set file_handle [open $file_name "r"]
lappend list_var [reads $file_handle]
close $file_handle
}

Is this the only way to do it, is there any shortcut to do it like in
unix we can do "cat *.log > filename" in tcl?

Thanks


0
5/25/2011 2:16:34 PM
comp.lang.tcl 23428 articles. 2 followers. Post Follow

1 Replies
921 Views

Similar Articles

[PageSpeed] 34

At Wed, 25 May 2011 07:16:34 -0700 (PDT) sunny <techbie.wordpress@gmail.com> wrote:

> 
> Hi All,
> 
> I am a newbie to tcl and i am having problem performing some of basic
> operations using tcl.
> 
> I am having a centralized location in server where i am large number
> of directories with all user logins and each directory is having some
> log files with *.log extension. My requirement is to identify
> directories with my login id and then go into each directory and
> append all the information available in *.log files into one file.
> 
> Problem 1:
> 
> For identifying directory name with my login, i am using
> 
> exec ls | grep <USERNAME>
> 
> But it is returning "child process exited abnormally"
> 
> If i run exec ls only, it lists down all the directories.
> 
> What is wrong with "exec ls | grep <USERNAME>"
> 
> Other related question, in tcl is there any inbuilt command that can
> be used to grep directories with a particular login like file command?

look at the glob and regexp commands:

set username heller
glob -nocomplain "*${username}*

or

foreach f [glob -nocomplain *] {
  if {[regexp {some-USERNAME-pattern} $f] > 0} {
    process $f
  }
}

(some-USERNAME-pattern would be a regexp pattern expression and the
procedure 'process' would be some code that does something with the
matched filename.)

> 
> 
> Problem2:
> 
> Other question is regarding opening multiple files and dumping in one
> file.
> 
> I am trying following code
> 
> set file_handle [open "*.log" "r"]

open does not take a wildcard!

> 
> But is giving error.
> 
> So i think i should do it like
> 
> set list_dir [glob *.log]
> foreach file_name $list_dir {
> set file_handle [open $file_name "r"]
> lappend list_var [reads $file_handle]
> close $file_handle
> }
> 
> Is this the only way to do it, is there any shortcut to do it like in
> unix we can do "cat *.log > filename" in tcl?

The '*.log' is processed by shell wildcard expansion.  Cat merely takes
multiple arguments.

proc cat {args} {
  set result {}
  foreach f $args {
    set infp [open $f r]
    append  result [read $infp]
    close $infp
  }
  return $result
}

set alllogs [eval cat [glob *.log]]



> 
> Thanks
> 
> 
>                                                                                                                                 

-- 
Robert Heller             -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software        -- http://www.deepsoft.com/
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments


                         
0
heller (3031)
5/25/2011 3:14:42 PM
Reply: