在txt文件中查找重复字符串,并使用相同名称修改的格式进行修改

时间:2013-09-26 05:28:24

标签: regex string tcl

请帮助:我的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"
    } 
} 

2 个答案:

答案 0 :(得分: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

这允许您检查任何行。如果你有大文件,处理时间会很快。

答案 1 :(得分: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