LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2007, 04:03 AM   #1
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
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
 
Old 09-13-2007, 04:40 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
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
 
Old 09-13-2007, 04:42 AM   #3
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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
 
Old 09-13-2007, 05:47 AM   #4
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
sed -i '/disable_from_plugins/d' munna.c
 
Old 10-03-2007, 04:24 AM   #5
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by /bin/bash View Post
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
 
Old 10-03-2007, 05:03 AM   #6
Nimoy
Member
 
Registered: Jun 2003
Location: Currently Denmark
Distribution: Ubuntu 15.04
Posts: 336

Rep: Reputation: 30
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
 
Old 10-03-2007, 05:21 AM   #7
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Nimoy View Post
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
 
Old 10-03-2007, 05:49 AM   #8
Nimoy
Member
 
Registered: Jun 2003
Location: Currently Denmark
Distribution: Ubuntu 15.04
Posts: 336

Rep: Reputation: 30
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...

Last edited by Nimoy; 10-03-2007 at 06:25 AM. Reason: Correction clean up to clear misunderstanding
 
Old 10-03-2007, 06:09 AM   #9
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Nimoy View Post
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
 
Old 10-03-2007, 06:22 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Perhaps you should at least read "sed --help". Note the "-i" option.
 
Old 10-03-2007, 07:58 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
My all-time favorite tutorial on SED:
http://www.grymoire.com/Unix/Sed.html
 
Old 10-03-2007, 08:29 AM   #12
Nimoy
Member
 
Registered: Jun 2003
Location: Currently Denmark
Distribution: Ubuntu 15.04
Posts: 336

Rep: Reputation: 30
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....

Last edited by Nimoy; 10-03-2007 at 10:40 AM. Reason: Unless comment added - and exchanged " for ' + added the filename
 
Old 10-03-2007, 09:56 AM   #13
Nimoy
Member
 
Registered: Jun 2003
Location: Currently Denmark
Distribution: Ubuntu 15.04
Posts: 336

Rep: Reputation: 30
One little thing to notice

I removed the / in front of rame....
 
Old 10-04-2007, 03:23 AM   #14
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Nimoy View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Line Help: Remove a List of Files lukasbradley Linux - Newbie 2 08-17-2005 04:21 AM
Please, Remove the automatically generated line in the Signatures john_walsh54 LQ Suggestions & Feedback 16 06-05-2005 12:34 PM
remove folder from command line demmylls Linux - General 3 02-15-2004 09:52 AM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 01:07 PM
How to remove line of text from file netkepala Linux - General 2 05-23-2003 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration