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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-30-2006, 02:08 AM
|
#1
|
Member
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700
Rep:
|
Strip comments from a file
I have a file, config.cfg. All lines beginning with "#" are comments (whilst a line of the form: is not.). I need to strip all comments from the file from the bash prompt. Any ideas?
|
|
|
09-30-2006, 02:23 AM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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
|
|
|
09-30-2006, 05:28 AM
|
#3
|
Member
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424
Rep:
|
But here's sed:
Code:
sed -i.bak -e '/^#/d' filename
It makes a backup of the original file.
|
|
|
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
|
Delete lines beginning with a hashmark.
Edit:
This one for blank lines.
Last edited by //////; 09-30-2006 at 09:27 AM.
|
|
|
09-30-2006, 10:40 AM
|
#5
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
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
|
|
|
09-30-2006, 10:51 AM
|
#6
|
Senior Member
Registered: Nov 2002
Location: British Columbia, Canada
Distribution: Gentoo x86_64; FreeBSD; OS X
Posts: 3,764
Rep:
|
|
|
|
09-30-2006, 12:39 PM
|
#7
|
Member
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207
Rep:
|
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
|
|
|
09-30-2006, 12:56 PM
|
#8
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Thanks Dan04!
|
|
|
09-30-2006, 03:29 PM
|
#9
|
Senior Member
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,639
Rep:
|
Or with perl:
Code:
perl -n -i.bak -e "print unless /^#/;" test.dat
This one also creates a backup.
Brian
|
|
|
10-02-2006, 05:05 PM
|
#10
|
Member
Registered: May 2002
Posts: 964
Rep:
|
Code:
sed 's/#.*//;/^$/d' filename > newfilename
|
|
|
All times are GMT -5. The time now is 03:18 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|