-2

i'm kinda new to linux programing, and I've searched everywere, and i don't find any answer for my question, i have a file lets call it, config.txt/.ini; My question is: Is there anyway with a script, to find in the file some text and if it finds the search text do something;


For exemple:

  • Search for: 'my/text/mytext'
  • And add: ';' to the begin of the line.
  • or even delete the line.
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
Hugo Alves
  • 79
  • 7

2 Answers2

0

Have you considered looking at tools such as:

  • awk
  • sed
  • perl
  • python

which all can do this fairly easily.

Awk is probably the slimmest (and thus fastest) of these:

awk '{sub(/root/, "yoda"); print}'

will substitute the first match for regexp root with the string yoda on each line.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • And to delete the line what could it be like? – Hugo Alves May 29 '13 at 23:14
  • Read the `awk` manual. That is ALL in there. Put a filter before the `{` – Has QUIT--Anony-Mousse May 29 '13 at 23:14
  • I would add `sed` to the list – Lorkenpeist May 29 '13 at 23:16
  • Well i tried sed and awk, but awk gives me some wierd error, and sed just makes that file empty, i want some script that does something like this: `sed 's/addons\/mani_admin_plugin\/bin\/sourcemod_mm/ /' addons/metamod/metaplugins.ini > addons/metamod/metaplugins.ini` With this i mean it to search for the string "addons/mani_admin_plugin/bin/sourcemod_mm", and remove it or replace it with a "space" (empty character). – Hugo Alves May 30 '13 at 01:44
  • Just don't *overwrite* the file the very moment you want to read from it. Use a temporary file, or the `-i` (inplace) switch. – Has QUIT--Anony-Mousse May 30 '13 at 10:23
0

Since your question is vague, and you didn't define what kind of script, and because I'm currently learning Python, I took the time to write a python script to remove lines in foo.txt that contain "mytext". Yes, it is possible. There are countless other ways to do it as well.

import re

# Open the file and read all the lines into an array
f = open("foo.txt", "r")
lines = []; 
for line in f:
    lines.append(line)
f.close()

# Write all the lines back that don't match our criteria for removal
f = open("foo.txt", "w")
for line in lines:
    if re.search("mytext", line) == None:
    f.write(line)
f.close()
PherricOxide
  • 15,493
  • 3
  • 28
  • 41
  • I think it's right this, so it will only remove the lines with "mytext" on them right? – Hugo Alves May 29 '13 at 23:18
  • Yep. It will read all the lines in, and open the file with "w", which will delete anything inside it already, and then write out all the lines that don't contain "mytext". – PherricOxide May 29 '13 at 23:28
  • Hum ok i got it, but it gives me this errors relatives to the "f = open(..." ` line 21: syntax error near unexpected token `(' line 21: ` f = open("plugins.ini", "r");' ` – Hugo Alves May 29 '13 at 23:39
  • Why did you put a ' at the end? – PherricOxide May 30 '13 at 21:10