LinuxQuestions.org
Visit Jeremy's Blog.
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 07-28-2007, 12:52 PM   #1
khairilthegreat
LQ Newbie
 
Registered: Oct 2006
Distribution: Kubuntu
Posts: 25

Rep: Reputation: 15
using sed to delete line to the right


I have a configuration file which had so many comment that I decided to get rid of the comment. The problem is I don't know how to automatically delete the comment.

Suppose I have file config.txt with content:

Code:
; this is comment
; this is comment
; this is comment
a=b+c
go to page 100    ;This is comment
create file
set type=MX       ;This is comment
get handles
; this is comment
; this is comment
I could run command like this

Code:
cat config.txt | sed '/;/d' | sed '/^$/d' > config2.txt
This will produce file config2.txt with content

Code:
a=b+c
create file
get handles
Note that the

Code:
go to page 100    #This is comment
set type=MX       #This is comment
is missing, because sed search every line contain the ; symbol and delete it. What is the command be so the output of config2.txt will be:

Code:
a=b+c
go to page 100
create file
set type=MX
get handles
Thanx in advance.

--------------
Note:
I also have tried the solution in "similar thread" at this forum, but it doesn't work for me.
 
Old 07-28-2007, 01:26 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I simplified and added a few lines......
Code:
mherring@6[play]$ cat comment
; this is comment
a=b+c go to page 100 ;This is comment
create file
set type=MX ;This is comment
;
;j
;hh
mherring@6[play]$ cat comment|sed 's/;.*$//g'

a=b+c go to page 100
create file
set type=MX
This particular sed expression means:
<<find ";" followed by any # of characters to the end of the line, and replace with ""(nothing)>>.

In this example, the $ (end of line) is not required---ie this:
sed 's/;.*//g'
does the same thing. (This is presumably because sed operates one line at a time.)

Last edited by pixellany; 07-28-2007 at 01:29 PM.
 
Old 07-28-2007, 01:39 PM   #3
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 476
Blog Entries: 116

Rep: Reputation: 74
Probably the easiest solution would be to use the right tool...cut. Here's what I have:

Code:
user@host:~$ cat ./testfile.txt
; this is comment
; this is comment
; this is comment
a=b+c
go to page 100 ;This is comment
create file
set type=MX ;This is comment
get handles
; this is comment
; this is comment
user@host:~$
The "cut" command allows you to select specific regions of lines (-f1 = field 1, which is the field before the delimiter is encountered), with the regions delimited by the -d';' option. You could use any delimiter you want (space is default, I believe).
Code:
user@host:~$ cut -d';' -f1 ./testfile.txt



a=b+c
go to page 100
create file
set type=MX
get handles


user@host:~$
Note that it'll have blank lines, so you could pipe the output to sed to remove blank lines if you want...
Code:
user@host:~$ cut -d';' -f1 ./testfile.txt | sed '/^$/d'
a=b+c
go to page 100
create file
set type=MX
get handles
user@host:~$
Edit - I know I'm being nit-picky, but you can do away with the initial "cat" command and just append the input file to the end of "sed"...i.e.
Code:
user@host:~$ sed '/^$'/d' ./testfile.txt

Last edited by rocket357; 07-28-2007 at 02:04 PM.
 
Old 07-28-2007, 01:56 PM   #4
Daws
Member
 
Registered: May 2006
Location: UK
Distribution: Debian, Linux From Scratch
Posts: 416
Blog Entries: 1

Rep: Reputation: 30
Code:
sed -i.bak -e 's/;.*//g' -e '/^$/d' configfile
Nah sed ftw. (NB this edits the file in place while creating a backup (configfile.bak)

EDIT: used quote tags by accident

Last edited by Daws; 07-28-2007 at 01:58 PM.
 
Old 07-28-2007, 01:57 PM   #5
khairilthegreat
LQ Newbie
 
Registered: Oct 2006
Distribution: Kubuntu
Posts: 25

Original Poster
Rep: Reputation: 15
It works

Thank's all for the fast response.

Both solution works really great.
 
Old 07-28-2007, 02:10 PM   #6
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 476
Blog Entries: 116

Rep: Reputation: 74
Quote:
Originally Posted by Daws
Code:
sed -i.bak -e 's/;.*//g' -e '/^$/d' configfile
Nah sed ftw. (NB this edits the file in place while creating a backup (configfile.bak)
heh. Thanks for pointing out my short-sightedness...yes, a single call to sed will do the trick here.
 
  


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
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 04:41 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM


All times are GMT -5. The time now is 06:10 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration