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-30-2006, 02:08 AM   #1
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Strip comments from a file


I have a file, config.cfg. All lines beginning with "#" are comments (whilst a line of the form:
Code:
aaa # something
is not.). I need to strip all comments from the file from the bash prompt. Any ideas?
 
Old 09-30-2006, 02:23 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
an sed/awk solution will be posted soon enough, but i like to do it in Python
Code:
#!/usr/bin/python
import fileinput
for lines in fileinput.FileInput("data.bak",1): #inplace edit
 	lines = lines.strip() #strip newlines
 	if lines.startswith("#"):
 		lines = lines[1:]
 	print lines
 
Old 09-30-2006, 05:28 AM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
But here's sed:
Code:
sed -i.bak -e '/^#/d' filename
It makes a backup of the original file.
 
Old 09-30-2006, 09:23 AM   #4
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Pop!_OS && Windows 10 && Arch Linux
Posts: 830

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
Code:
grep -v '^#'
Delete lines beginning with a hashmark.

Edit:

Code:
grep -v '^$'
This one for blank lines.

Last edited by //////; 09-30-2006 at 09:27 AM.
 
Old 09-30-2006, 10:40 AM   #5
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I was working on a python oneliner but, I don't know how to tell it only comments at the beginning of lines. Waiting for python guru input.
Code:
python -c "import sys; print sys.stdin.read().replace('#', '')," < file.txt
 
Old 09-30-2006, 10:51 AM   #6
bulliver
Senior Member
 
Registered: Nov 2002
Location: British Columbia, Canada
Distribution: Gentoo x86_64; FreeBSD; OS X
Posts: 3,764
Blog Entries: 4

Rep: Reputation: 78
I wrote this a while ago:
http://badcomputer.org/unix/code/cfgcl.bot
 
Old 09-30-2006, 12:39 PM   #7
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by homey
I was working on a python oneliner but, I don't know how to tell it only comments at the beginning of lines. Waiting for python guru input.
Code:
python -c "import sys; sys.stdout.writelines(line for line in sys.stdin if not line.strip().startswith('#'))" < file.txt
 
Old 09-30-2006, 12:56 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Thanks Dan04!
 
Old 09-30-2006, 03:29 PM   #9
BCarey
Senior Member
 
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,639

Rep: Reputation: Disabled
Or with perl:

Code:
perl -n -i.bak -e "print unless /^#/;" test.dat
This one also creates a backup.

Brian
 
Old 10-02-2006, 05:05 PM   #10
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
sed 's/#.*//;/^$/d' filename > newfilename
 
  


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
Help Strip / Remove attachments (Replace with TXT file if Possible) Vince0000 Linux - General 2 09-17-2010 07:46 PM
a file without comments basak Linux - Software 2 08-08-2006 07:20 AM
looking for script - strip out all # comments itsjustme Programming 3 03-11-2004 11:57 AM
How sed strip /* comments aaronzh Programming 1 06-05-2003 06:16 PM
application for file comments/descriptions Serena Linux - Software 1 05-01-2002 01:05 PM

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

All times are GMT -5. The time now is 03:18 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
Open Source Consulting | Domain Registration