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

Extract certain values from string in .txt files with PowerShell
Apache 301 Redirect Add language prefix if not present
Regex, match characters and doesn't contain more than one _ followed by another _
Regex ignore word
Regex not valid in google sheets error?
extract substring using regex in shell script
Eclipse Add text to first line of all files
Renaming files by using a prefix from text file
Python regex to find words, which also excludes particular words
Regex - exclude search results
Update a line in the AD info field
Sed Match Number followed by string and return Number
Perl matching string with mix of alphanumeric,_ and any number of square braces [closed]
Bash: extract the scheme and host part from a URL string
RexExp find a word within a list of words that are within a parenthesis
Converting EBNF to Regular Expression

Categories

HOME
clojure
web-services
dynamics-crm-365
react-navigation
parse.com
path
bootstrap-selectpicker
redhat
xquery
box
regression
py.test
proftpd
condor
python-2.5
installer
phpbb3
aerospike
silk-performer
h.264
realm-java
rgdal
py4j
nested-loops
launchpad
ruamel.yaml
json-c
background-process
spring-websocket
rhel7
perl-module
vue2
solid-principles
drupal-webform
bem
compass-lucene
word-embedding
polymer-cli
angular2-seed
edeliver
projection
zfs
win2d
couchbase-lite
hostapd
default-browser
scoring
wrk
ios-app-group
bosh-deployer
omniauth
postmark
measurement-protocol
printscreen
agents-jade
webclient-download
cufft
singly-linked-list
isapi-rewrite
highslide
google-api-ruby-client
cucumber-java
subroutine
ssha
jedi
ospf
realm-list
movie
epplus-4
newsql
imake
jasmin
typesafe-activator
repaint
citymaps
lwuit-list
dynamic-typing
wmv
sirtrevor
android-c2dm
vlfeat
gora
rte
cocosbuilder
squirrelmail
ariatemplates
google-website-optimizer
httpcontext.cache
f2c
springboard
retain
coolstorage
rails-3.1
unitils
cracker
gendarme
sqlprofileprovider

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