regex


Find the repeating string in the txt file and modifiy with samename_modified format


Please help: my txt file contains these kind of data.
I need to find the repeating string and if the string repeating then modify the post_suffix with:
a
b
st1
ver1
st2
ver2
st3
st4
st_list1
ver3
ver4
ver_list1
st1
ver5
st2
ver5
st1
ver6
Oitput file should be like this:
a
b
st1
ver1
st2
ver2
st3
st4
st_list1
ver3
ver4
ver_list1
st1_repeted1
ver5
st2_repeted1
ver5_repeted1
st1_repeted2
ver6
My code:
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"
}
}
Not sure how your code exactly should work and it seems it will check only for st1
I would use something like this:
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
This allows you to check for any lines. If you have large files though, the processing time will shoot up.
My take on this would be
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

Related Links

Grepping a 20g file in bash
What Perl matching regexp nuances can cause headaches?
What is the invalid URI error from SpamAssassin actually matching?
Validate postal cade using Regex on each character
VBA regex - match an expression that doesn't begin with a particular word
How to pass variable to regular expression in PERL
Batch rename screen shots on Mac OS X
US Phone regex that works in IE
Regex with lookaround for Lazarus
codeigniter regular expression for uri slug in routes
regex to match strings not preceded by a bang
What would the regex to match the hostname of this string?
How do I do a regex search and replace in sublime text 2?
Why are these regular expressions different?
r regex mileage
What is the complexity (Big-O) of searching an indexed data in mongoDB?

Categories

HOME
math
dom
simulated-annealing
google-apis-explorer
xaml
crf
x-editable
servlets
appium-ios
apache-pig
apacheds
path
fonts
angular-mdl
regression
jena
struts2-jquery
openframeworks
pymc3
sqlconnection
session-cookies
resolve
asp.net-mvc-5.2
numericupdown
googletest
apache-kafka-streams
factory-girl
records
teraterm
drupal-commerce
silk-performer
freetype
stm32f4discovery
jruby
sencha-touch-2
cadvisor
easendmail
physics
solarwindslem
pmd
jsplumb
apache-commons-httpclient
chai
jformattedtextfield
chamilo-lms
api-doc
increment
amazon-vpc
nomenclature
procobol
btle
polymer-cli
hostname
free-diameter
android-studio-2.1
hostapd
requirements-management
ssms-2012
paypal-webhooks
zbar
webloadui
omniauth
kie-workbench
jasny-bootstrap
ellipsis
pygments
blpapi
schematiq
storage-duration
membership
sketching
smarty2
openxls
java-client
reactive-cocoa-3
android-design-library
android-tablelayout
applicationstate
excel-charts
chatjs
typesafe-activator
pkcs#10
nhunspell
angularjs-google-maps
declspec
ramdisk
dfsort
blueprint
ioerror
programmers-notepad
memoization
ccombobox
asp.net-authentication
guice-servlet
godaddy-api
locomotivejs
blackberry-cascades
browserid
guvnor
onmouseout
ribbon-control
maven-bundle-plugin
force.com
proxy-classes
gemstone
scramble
iequalitycomparer
cracker
code-statistics
nhaml
virtual-server
case-tools

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App