i, nice place
could you extend your explanation with:
-add/replace/del a midle line (special replace)
-replace a word in a line (let's say file as 3 lines and you want to change second word of second line)
-read a specific word on a line (could be same as abose 2 of 2)
tks extend this faq and make a guide/tutorial hould be great, nice and simple explenations here.
Hi - one more 'basic' question on this (I've searched the forums but still havn't got it) - how would you go about reading a specific line number (as there's nothing unique in the text I can identify it with)
eg here's the script but using a string match, but instead, I want to get line number 13...
Just like to state this is a really great forum, actually the most imformative i've been on, btw not brown nosing. lol
To my question:
I am attempting to write a TCL/TK firewall gui on my fedora core box, just having bit of a problem with deleting strings from a file, I want to pattern match a string, and if it exists in a particular file i would like to delete it. I understand how to delete lines from a file (from the examples above), but just don't know how to go about matching a string and removing it. any help would be very appreciated.
sorry that was proberly a bit vague,
############################
#this is the check button command#
############################
if {$http == 1} {
set a [open /iptab a+]
puts $a "iptables -A OUTPUT -o \$internet -p tcp" <--Inputs iptables command into file
close $a
} else {
#########################################
# this is the bit where i'd like the pattern matching #
#########################################
so, for example if "iptables -A OUTPUT -o $internet -p tcp" existed in the file, i'd like to remove it from the file.
so, basically selecting the tick box turns a feature on, deselecting it turns it off
}
Sorry if its a particulary difficult post to answer
if [string match "iptables -A OUTPUT -o \$internet -p tcp" $line] { # it's a match, delete..
yeah thats the thing i want to acheive, but just don't know how to go about the deletion part. i've been using the examples of how to open a file, extract the text, and delete specified numbered lines.
but what about a specified string of text?
example of my code:
set fname "/iptab"
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
foreach lines [split $data "\n"] {
if {[string match "iptables -A OUTPUT -o \$internet -p tcp" $line]} {
set line_to_delete $line}
#so line_to_delete contains the string i want to delete, but where from here??
}
set fp [open $fname w]
puts $fp [join $lines "\n"]
close $fp } }
set fname "/iptab"
set fp [open $fname "r"]
set data [read -nonewline $fp]
close $fp
set lines [split $data "\n"]
set linenum 0
foreach line $lines {
if [string match -noc "*iptables -A OUTPUT -o \$internet -p tcp*" $line] {
set lines [lreplace $lines $linenum $linenum]
set fp [open $fname "w"]
puts $fp [join $lines "\n"]
close $fp
}
incr linenum
}
Hi, i've got a smal tcl code that reads the contents of a file for devices, eth0, eth1 etc...
the devs file contains all of the items in the list, but it only returns the last item that matches in the dev file, i know its something to do with the IF's, but i just can't get my head arounfd the correct layout. could any suggest the correct layout?
set devices "/devs"
set file [ open $devices r ]
set data [read -nonewline $file]
close $file
set lines [split $data "\n"]
foreach line $lines {
if [string match "*lo:*" $line] {
set lo lo
} else { set lo ""}
if [string match -noc "*eth0:*" $line] {
set eth0 eth0
} else { set eth0 ""}
if [string match -noc "*eth1:*" $line] {
set eth1 eth1
} else { set eth1 ""}
if [string match -noc "*ppp0:*" $line] {
set ppp0 ppp0
} else { set ppp0 ""}
if [string match -noc "*ppp1:*" $line] {
set ppp1 ppp1
} else { set ppp1 ""}
}
This should of been asked in the Script Help section NOT the FAQ section. Please dont post your script help questions in this section.
The lifecycle of a noob is complex. Fledgling noobs gestate inside biometric pods. Once a budding noob has matured thru gestation they climb out of their pod, sit down at a PC, ask a bunch of questions that are clearly in the FAQ, The Noob is born