LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2009, 11:15 PM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Delete a line from a file


Hi guys,

How do i write a script which will delete a perticular line from a file starting with "shell" word.
 
Old 11-04-2009, 11:21 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what have you learnt till now since you started 70 posts ago? Put in some effort and show us some code. I ask because this is a very simple task.
 
Old 11-05-2009, 12:01 AM   #3
simer_anand88
LQ Newbie
 
Registered: Oct 2009
Distribution: Mandriva
Posts: 25

Rep: Reputation: 15
what is the script? code??

Last edited by simer_anand88; 11-05-2009 at 12:02 AM.
 
Old 11-05-2009, 03:17 AM   #4
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
I m explaining in detail.

Below is the content of my file called vm.cfg.
I need to delete a line starting from disk word so when i write
sed "/^disk/d" vm.cfg
It only delete the line
disk = ['file:/var/ovs/mount/93B14928C7A6438284753B2F2AB197BB/seed_pool/vm_temp
and not
/System.img,hda,w',
'file:/OVS/iso_pool/winxpsp2/WXPVOL_EN.iso,hdc:cdrom,r',
]
How can i establish this.
Code:
[root@OVM-SERVER1 vm_temp]# cat vm.cfg
acpi = 1
apic = 1
builder = 'hvm'
device_model = '/usr/lib/xen/bin/qemu-dm'
disk = ['file:/var/ovs/mount/93B14928C7A6438284753B2F2AB197BB/seed_pool/vm_temp/System.img,hda,w',
'file:/OVS/iso_pool/winxpsp2/WXPVOL_EN.iso,hdc:cdrom,r',
]
kernel = '/usr/lib/xen/boot/hvmloader'
memory = '300'
name = 'vm_temp'
on_crash = 'restart'
on_reboot = 'restart'
pae = 1
serial = 'pty'
timer_mode = '0'
usbdevice = 'tablet'
uuid = '8569c556-fdf9-41f7-93f4-52350560b13c'
vcpus = 1
vif = ['bridge=xenbr0,mac=00:16:3E:64:FB:D3,type=ioemu']
vif_other_config = []
vnc = 1
vncconsole = 1
vnclisten = '0.0.0.0'
vncpasswd = 'oracle'
vncunused = 1
 
Old 11-05-2009, 03:22 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
there are many ways beside sed to delete a line, here's one other way.
Code:
$ awk '!/^disk/' file  > newfile
I suggest you start learning awk from now on. see my sig
 
Old 11-05-2009, 03:43 AM   #6
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by ghostdog74 View Post
there are many ways beside sed to delete a line, here's one other way.
Code:
$ awk '!/^disk/' file  > newfile
I suggest you start learning awk from now on. see my sig
disk = ['file:/var/ovs/mount/93B14928C7A6438284753B2F2AB197BB/seed_pool/vm_temp/System.img,hda,w',
'file:/OVS/iso_pool/winxpsp2/WXPVOL_EN.iso,hdc:cdrom,r',
]
Thanks for your help but.
I want to write a program which will delete above mentioned set of lines.
 
Old 11-05-2009, 07:54 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
There will be no SED vs. AWK wars.......

Have you read a tutorial on SED? I ask this because you used an address/action statement earlier---what you need is an address range, like so:

Code:
sed '/start/,/finish/ d'   ##deletes all lines, beginning with on containing "start", and ending on one containing "finish".

sed '/start/,+3 d'   ##as above, but deletes 3 lines after the first address
Best SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 11-05-2009, 08:06 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pinga123 View Post
disk = ['file:/var/ovs/mount/93B14928C7A6438284753B2F2AB197BB/seed_pool/vm_temp/System.img,hda,w',
'file:/OVS/iso_pool/winxpsp2/WXPVOL_EN.iso,hdc:cdrom,r',
]
Thanks for your help but.
I want to write a program which will delete above mentioned set of lines.
Code:
$ awk '/^disk/{f=1}f&&!/\]/{next}f&&/\]/{f=0}f==0' file
or
Code:
$ awk '/disk/,/\]/{next}1' file

Last edited by ghostdog74; 11-05-2009 at 08:12 AM.
 
  


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
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07:46 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 06:34 PM
Read and delete line from file chloraldo Programming 8 08-18-2006 06:56 AM
Delete line from file nebbus Linux - Newbie 3 06-03-2006 01:46 PM
How do I delete a line in a file with fstream (C++)? CrazyHoboMan Programming 3 08-18-2005 03:06 AM


All times are GMT -5. The time now is 08:31 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration