tcl - Delete line regex is found on -


i looking iterate through file, finding patterns, if pattern found, removing entire line on.

this have:

#  slurp data files set fp [open "params.txt" r] set file_data [read $fp] close $fp  set fp [open [lindex $argv 0] r ] set configfile [read $fp]  close $fp  #  process data file set data [split $file_data "\n"]     foreach line $data {      # line processing here     if { "$data" != "" } {           if { [ regexp {\b"$data"\b} $configfile ] == 1 } {             #remove entire line regex found on in $configfile         }     } } #write $configfile file 

the script going have 1 argument, "haystack," params.txt hardcoded list of "needles." issue regex returns true or false, don't know how proper line, , remove whole thing.

what makes trickier of items in params.txt "words" inside things not wish delete (example below)

apple starts "a" applepie delicious pineapple delicious 

i want delete line apple starts "a" no touch others. "words" because have things get parameters "word" want find , remove.

if assume words in params.txt letters (+ maybe digits , underscores, i.e., not re metasyntax), 1 per line, can with:

# read in config set fp [open "params.txt"] set words [split [read $fp] "\n"] close $fp  # read in data set fp [open "inputdata.txt"] set lines [read $fp] close $fp  # process foreach word $words {     regsub -all -line "^.*\\y$word\\y.*(?:\n|$)" $lines "" lines }  # write out set fp [open "outputdata.txt" w] puts -nonewline $fp $lines close $fp 

the real key this:

regsub -all -line "^.*\\y$word\\y.*(?:\n|$)" $lines "" lines 

the interesting bits are:

  • -line, puts regsub in line-matching mode instead of default “whole string matching” mode (affecting how ^, . , $ work),
  • -all, makes regsub replace possible matches instead of first one,
  • \\y, \y re engine, , matches word boundary, and
  • (?:\n|$) matches newline @ end or end of string/line, line removed , not cleared.

if each line in config more general that, need work make “nice” before using in regsub. fortunately, regsub right tool it!

foreach word $words {     # i'm trimming lines; might want not that...     regsub -all {\w} [string trim $word] {\\&} subre     regsub -all -line "^.*\\y$subre\\y.*(?:\n|$)" $lines "" lines } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -