Operating System - Microsoft
1748154 Members
3678 Online
108758 Solutions
New Discussion юеВ

Re: Need help on TCL scripts

 
Matthew_50
Valued Contributor

Need help on TCL scripts

Hello,

can I use sed operation in tcl scripts ?

Need to get line 20 to line 60.
Pattern might be vary.

Thanks.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Need help on TCL scripts

Hi Matthew:

Since TCL scripts are whitespace-delimited strings terminated by a newline or semicolon, they are appropriate text files for use by standard tools like 'sed', 'awk', Perl, etc.

As always, when in doubt, TRY IT! :-)

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Need help on TCL scripts

Hi (again) Matthew:

> Need to get line 20 to line 60. Pattern might be vary.

I suppose that that really was your question. You can do:

# sed -ne '20,60p' file

THe '-n' turns off printing. The '-e' says the script follows. The '20.60p' says from line-20 (one-relative) through line-60 print ('p').

If you wanted to do the same not knowing line-numbers but knowing patterns that mark the lines you want, you could do:

# sed -ne '/pattern1/,/pattern2/p' file

Notice that the regular expression to match is enclosed in forward slashes, with a common again signifying a range.

Regards!

...JRF...
Matthew_50
Valued Contributor

Re: Need help on TCL scripts

hmm, due the customer enviornment is windows, try to solve this in perl,

but wondering, in pervious perl which you had provide,

#!/usr/bin/perl

use strict;
use warnings;

my ( @first, @second, $slot, $n );
while (<>) {
next if /^===/;
if (m{^\s*\d}) {
@first = split;
next;
}
if (m{^slot}) {
@second = split;
$slot = shift @second;
( $slot = shift @second ) =~ s/://;
}
if ( m{^\s*$} or eof ) {
for ( $n = 0; $n < @first; $n++ ) {
print $slot, '/', $first[$n],' ',$second[$n],"\n";
}
}
}
1;

I need to check if $second[$n] contain k or m, if contain k, then $second[$n], exclude the string k, and multiplier 1000, if contain m, then exclude the string m, and multiplier 1000000, if not contain k either m, print, any suggestion ?

originally, I would like to combine tcl with expect module to get the input, (perl in windows do not have expect), then call perl script to handle to reformat, then called cygwin to process the numbers to re-print the result,

don't know if there have any other quick way which not to install too many extra application on windows system.

kobylka
Valued Contributor

Re: Need help on TCL scripts

Hello matthew!

You can access all the traditional regexps in TCL, plus AREs, which include some extra definitions.

To read in lines 20 to 60 in a script and process each line using patterns you could easily:

set fd [open "file_name" "r"]
# Slurp file contents
set lines [split [read $fd] "\n"]
close $fd
# Get lines 20 - 60 inclusive
set lines [lrange $lines 20 60]

# Operate on lines
foreach line $lines {
# Use regexp to match lines by pattern
# Use regsub to substitute by pattern
}

The exact syntax and regexp rules are described here:

http://www.tcl.tk/man/tcl8.4/TclCmd/contents.htm


Kind regards,

Kobylka
James R. Ferguson
Acclaimed Contributor

Re: Need help on TCL scripts

Hi (again):

> hmm, due the customer enviornment is windows, try to solve this in perl, but wondering, in pervious perl which you had provide,

Wait. Let's regroup! This thread specifically asked about 'sed'. I answered that.

> originally, I would like to combine tcl with expect module to get the input, (perl in windows do not have expect), then call perl script to handle to reformat, then called cygwin to process the numbers to re-print the result...don't know if there have any other quick way which not to install too many extra application on windows system.

Your real objective isn't clear. If the core issue is being able to munge text files on a Windows system, Perl is definitely the tool in my opinion. If your objective only needs to be met once, then you might be best servered by copying the source data to a Unix platform; massaging it; and copying ib back to the "other" environment.

As for the un-related (?) question you raised regarding your previous post and the Perl script I provided, any further discussion of that is best raised if you re-open _taht_ thread.

Regards!

...JRF...
Matthew_50
Valued Contributor

Re: Need help on TCL scripts

extract 17 - 57,

idealy to use,

perl -ne 'print 17 .. 57' show.log

James R. Ferguson
Acclaimed Contributor

Re: Need help on TCL scripts

Hi (again) Matthew:

If it helps, the 'sed' commands convert to Perl very easily:

# perl -ne 'print if 20..60' file

# perl -ne ''print if /pattern1/../patern2/'

As before (with 'sed') the first line of a file is numbered one.

Regards!

...JRF...
Matthew_50
Valued Contributor

Re: Need help on TCL scripts

Thanks to kobylka

set fd [open "show.log" "r"]
set lines [split [read $fd] "\n"]
close $fd

set lines [lrange $lines 16 56]
foreach line $lines {puts $line}