LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   remove the line (https://www.linuxquestions.org/questions/programming-9/remove-the-line-584357/)

munna_dude 09-13-2007 04:03 AM

remove the line
 
hi all
how to remove the line
first we have to search the line
if the line exist remove the line
else
dont do anything.

for example "disable_from_plugins" , this is the word containing the line
search the line and delete.. programatically

can you please help me

thank you in advance

Mega Man X 09-13-2007 04:40 AM

Something like this would do. You didn't mention which language you are using, so I will give you the solution in a random language. I'm sure it will help you:

Code:

Public Function DeleteLine(fName As String, LineNumber As Long) _
    As Boolean
'Purpose: Deletes a Line from a text file

'Parameters: fName = FullPath to File
'            LineNumber = LineToDelete

'Returns:    True if Successful, false otherwise

'Requires:  Reference to Microsoft Scripting Runtime

'Example: DeleteLine("C:\Myfile.txt", 3)
'          Deletes third line of Myfile.txt
'______________________________________________________________
                     

  Dim oFSO As New FileSystemObject
  Dim oFSTR As Scripting.TextStream
  Dim ret As Long
  Dim lCtr As Long
  Dim sTemp As String, sLine As String
  Dim bLineFound As Boolean
 
  On Error GoTo ErrorHandler
  If oFSO.FileExists(fName) Then
    Set oFSTR = oFSO.OpenTextFile(fName)
    lCtr = 1
    Do While Not oFSTR.AtEndOfStream
        sLine = oFSTR.ReadLine
        If lCtr <> LineNumber Then
            sTemp = sTemp & sLine & vbCrLf
        Else
            bLineFound = True
           
        End If
        lCtr = lCtr + 1
    Loop
 
    oFSTR.Close
    Set oFSTR = oFSO.CreateTextFile(fName, True)
    oFSTR.Write sTemp
 
    DeleteLine = bLineFound
  End If
 

ErrorHandler:
On Error Resume Next
oFSTR.Close
Set oFSTR = Nothing
Set oFSO = Nothing

End Function


munna_dude 09-13-2007 04:42 AM

hi i tried up to this
Code:

cat munna.c | grep disable_from_plugins
the output is
Quote:

g_ref(disable_from_plugins);
then hoe to remove the line that containing "disable_from_plugins".
and save the file..

and
if the word not find....
dont do any thing..

i prefer c or script.

please help me

thank you in advance

/bin/bash 09-13-2007 05:47 AM

sed -i '/disable_from_plugins/d' munna.c

munna_dude 10-03-2007 04:24 AM

Quote:

Originally Posted by /bin/bash (Post 2890514)
sed -i '/disable_from_plugins/d' munna.c

hi
i have another problem with this
i would not like to remove the line.
just wanna do some changes in the line..


for example "disable_from_plugins" , this is the word containing the line
search the line and change the line i.e
rame("rr.disable_from_plugins", 2);
to
"rame("rr.disable_from_plugins", 1);"
and save by programatically

can you please help me

thank you in advance

Nimoy 10-03-2007 05:03 AM

simple SED script
 
This simple script takes the string "Copyright 1999-2005 - All rights reserved" from all the html files in the directory where it is run and replaces it with "Copyright 1999-2007 - All rights reserved" and makes a backup as well... Hope it is of use.

for file in *.html
do
cp $file $file.bak &&
sed 's/Copyright 1999-2005 - All rights reserved/Copyright 1999-2007 - All rights reserved/g' $file.bak >$file
done

munna_dude 10-03-2007 05:21 AM

Quote:

Originally Posted by Nimoy (Post 2911567)
This simple script takes the string "Copyright 1999-2005 - All rights reserved" from all the html files in the directory where it is run and replaces it with "Copyright 1999-2007 - All rights reserved" and makes a backup as well... Hope it is of use.

for file in *.html
do
cp $file $file.bak &&
sed 's/Copyright 1999-2005 - All rights reserved/Copyright 1999-2007 - All rights reserved/g' $file.bak >$file
done

it is not helpfull..
previously i used
sed -i '/disable_from_plugins/d' munna.c

to delete the line

but i would like to change the text in that line and save in that file only..
i dont wanna use redirecting file..

can you help me
like one line command to do this operation ...

thank you in advance

Nimoy 10-03-2007 05:49 AM

sed in one line
 
AN IMAGINARY FILE CONTAINING: /usr/local/bin
The file name of the imaginary file is old - The one line command to change this file is:

sed 's|/usr/local/bin|/common/bin|' <old >new

If you can do it without the redirect (I assume you mean <old >new I am uncertain...

munna_dude 10-03-2007 06:09 AM

Quote:

Originally Posted by Nimoy (Post 2911600)
AN IMAGINARY FILE CONTAINING: I would like to insert something here

The file name of the imaginary file is filetochange - The one line command to change this file is:

sed 's/would like to insert/have inserted/g' filetochange

And should produce the follwing change in the file filetochange

I have inserted something here


i tried like this
Quote:

[munna@localhost munna]$ sed 's/rame("rr.disable_from_plugins", 0);/rame("rr.disable_from_plugins", 2);/g' simple.txt

rame("rr.disable_from_plugins", 2);
[munna@localhost munna]$
the output printing in the terminal, not in to the file..
i would like to save the output in that file by replacing the line..

can you help me

thank you in advance

jschiwal 10-03-2007 06:22 AM

Perhaps you should at least read "sed --help". Note the "-i" option.

pixellany 10-03-2007 07:58 AM

My all-time favorite tutorial on SED:
http://www.grymoire.com/Unix/Sed.html

Nimoy 10-03-2007 08:29 AM

Try this
 
sed -i 's#rame("rr.disable_from_plugins", 0);#ame("rr.disable_from_plugins", 2);#g' simple.txt

Copy the line

Go to CLI

Press CTRL+SHIFT+V

Press <Enter>

Should work...

Unless the file contains the # I used as the delimiter....

Nimoy 10-03-2007 09:56 AM

One little thing to notice
 
I removed the / in front of rame....

munna_dude 10-04-2007 03:23 AM

Quote:

Originally Posted by Nimoy (Post 2911822)
I removed the / in front of rame....

ok... it is working in terminal but not in the program
Code:

s10=g_strconcat("sed -i 's/rame("rr.disable_from_plugins", 2);/rame("rr.disable_from_plugins", 1)/g' ","/home/munna/Desktop/simple.txt",NULL);
i compiled this saying some error
Quote:

simple.c: In function ‘main’:
simple.c:97: error: expected ‘)’ before ‘privacy’
how can i overcome this error?

or else can you me other solution for this?
i would like to change 1 instead of 2 in that perticular line
rame("rr.disable_from_plugins", 2);

how can i do this

please help me

thank you in advance


All times are GMT -5. The time now is 04:39 AM.