1

请帮助:我的 txt 文件包含这些类型的数据。我需要找到重复的字符串,如果字符串重复,则修改 post_suffix:

a
b
st1
ver1
st2
ver2
st3
st4
st_list1
ver3
ver4
ver_list1
st1
ver5
st2
ver5
st1
ver6

Oitput 文件应该是这样的:

a
b
st1
ver1
st2
ver2
st3
st4
st_list1
ver3
ver4
ver_list1
st1_repeted1
ver5
st2_repeted1
ver5_repeted1
st1_repeted2
ver6

我的代码:

if {$rec == "st1"} {
#st1 
incr count_set1 
if {$count_set1 == 1} {
    #puts $fd "$new"
    } else {
        set pr_st1 $rec$U$count_set1
        regsub $rec $content $pr_st1 new_set 
        puts $fd "$new_set"
    } 
} 
4

2 回答 2

1

不确定您的代码应该如何工作,而且它似乎只会检查st1

我会使用这样的东西:

set file [open "File.txt" r]
set output [open "output.txt" w]

set wordlist ""

while {[gets $file line] != -1} {
    set id [lsearch -regexp $wordlist "^$line \[0-9]+$"]
    if {$id == -1} {
        lappend wordlist "$line 0"
        puts $output $line
        puts "new"
    } else {
        set count [lindex [lindex $wordlist $id] 1]
        incr count
        puts $output "${line}_repeated$count"
        set wordlist [lreplace $wordlist $id $id "$line $count"]
        puts repeated
    }
}

close $file
close $output

这使您可以检查任何行。但是,如果您有大文件,则处理时间会增加。

于 2013-09-26T08:31:51.253 回答
1

我对此的看法是

set file [lindex $argv 0]
set f_in [open $file r]
set new ${file}.new
set f_out [open $new w]

array set count {}

while {[gets $f_in line] != -1} {
    if {[info exists count($line)]} {
        set line [format "%s_repeated%d" $line $count($line)]
    }
    puts $f_out $line
    incr count($line)
}

close $f_in
close $f_out

# backup the original file and move the new file into place
file link -hard $file ${file}.old
file rename -force $new $file
于 2013-09-26T15:22:33.403 回答