LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 02-14-2011, 06:17 AM   #1
Jykke
Member
 
Registered: Sep 2005
Posts: 168

Rep: Reputation: 16
How do I get rid of surplus comment lines with sed


I have a large text file and I have some comment lines in it beginning with $.

Now here is the challenge there are some lines
where there is actually a written comment after $ (with some blanks between)

Then there are lines with only $ on it - with maybe some trailing spaces.

I would like to delete these dummy lines...

Lets say from

$ This is valid comment
$
$
$
$
$ Another Valid Comment
$
$Thid valid one

should become
$ This is valid comment
$ Another Valid Comment
$Thid valid one


apparently
sed '/^\$\n/d' file

was not the solution although I thought it would be maybe it is the problem that after $ there might be some trailing spaces?
 
Old 02-14-2011, 06:35 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Hello,

I'm sure there will be shorter ways, but hey, I'm still learning too
Code:
sed 's/^\$\ //;/^\$/d;s/.*/\$\ &/' <yourfile>
This will get you the lines that begin with $ followed by a space. I'm sure there's a way the first two can be executed in one but didn't get it to work.

Kind regards,

Eric

EDIT: oops, mist the last line without the space. Will have to look a bit further

Last edited by EricTRA; 02-14-2011 at 06:37 AM.
 
Old 02-14-2011, 06:35 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ ruby -ne 'print if $_ !~ /^\$\s+$/' file
 
Old 02-14-2011, 07:07 AM   #4
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Hi,

Just came up with another one, that might just do the trick, please let me know if it does:
Code:
sed '/[^\$[:alnum:]]/!d' test
(if your filename is test and contains the following:
Code:
en dit is gewone tekst
$ This is valid comment
$
$
$
$
$ Another Valid Comment
$
$Thid valid one
The output will be:
Code:
en dit is gewone tekst
$ This is valid comment
$ Another Valid Comment
$Thid valid one
Playing around sometimes provides good things (I hope)

Kind regards,

Eric
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comment out next 40 lines with vi-editor? Ujjain Linux - Software 3 07-06-2009 02:46 AM
Comment lines 1-9, 11-19, 21-29 using Sed kushalkoolwal Programming 8 02-03-2009 05:02 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 09:25 AM
listing non-comment lines scbops Linux - Newbie 5 09-18-2007 09:55 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM


All times are GMT -5. The time now is 03:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration