Prev: Tcl and MATLAB (Was: Re: Please help : suppress error message from exec
Next: exec - problem with stderr
From: Sooraj S on 16 Jul 2010 08:39 Hi, I want to grep for a particular string in a file and get the value next to it. My data: user1: 11111111 user2: 22222222 code ====== set in [open "data.txt" r] while {[gets $in line] != -1} { # here i want to grep for user2 and get the value 11111111 as out put. } close $in
From: Uwe Klein on 16 Jul 2010 08:56 Sooraj S wrote: > Hi, > I want to grep for a particular string in a file and get the value > next to it. > > My data: > user1: 11111111 > user2: 22222222 > > code > ====== set in [open "data.txt" r] while {[gets $in line] >= 0} { foreach {user val} $line break set user [ string trimright $user : ] lappend ::stat(users) $user lappend ::stat($user,values) } close $in set ::stat(users) [ lsort -unique $::stat(users) ] # look if a user is present and get the values: if {[lsearch $::stat(users) $User] >=0} { # get values set values $::stat($User,values) puts stderr "$User : $values" } else { puts stderr "$User not found " } uwe
From: Sooraj S on 16 Jul 2010 10:19 Hi, Can i use the string match function for this..but my code does not work..how can i use the regular expressions here..? data.txt ======= alex: 11111 suxena: 22222 code ====== set user "alex" set in [open "data.txt" r] while {[gets $in line] != -1} { if {[string match $line /$user/] == 0} { puts "alex found"; } }
From: Uwe Klein on 16 Jul 2010 10:50 Sooraj S wrote: > Hi, > > Can i use the string match function for this..but my code does not > work..how can i use the regular expressions here..? > > data.txt > ======= > alex: 11111 > suxena: 22222 > > code > ====== > set user "alex" > set in [open "data.txt" r] > while {[gets $in line] != -1} { > if {[string match $line /$user/] == 0} { > puts "alex found"; > } > } > certainly but read the man page string match <pattern> <string> if {[string match ${user}:* $line ] == 0} { uwe
From: Sooraj S on 16 Jul 2010 11:43 Thanks for your help. Now i want to read and write to the same file. I want to get alex's current number and change it to 33333 my_code ======== set user "alex" set new_num "33333" set $in [open "data.txt" r+] while {[gets $in line] >= 0 } { if {[string match $user:* $line] == 1} { set len [string length $user] set old_num [string replace $line 0 $len ] puts "Old number :$old_num" //this will print 11111 # here i want to write $new_num to the file...how will i get the index# } } close $in
|
Next
|
Last
Pages: 1 2 Prev: Tcl and MATLAB (Was: Re: Please help : suppress error message from exec Next: exec - problem with stderr |