The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk, ignore first x number of lines. trey85stang Shell Programming and Scripting 8 05-21-2008 04:44 AM
Ignore some lines with specific words from file comparison jakSun8 Shell Programming and Scripting 2 03-12-2008 11:11 PM
How can I ignore only the lines which have # at the begining? csaha Shell Programming and Scripting 1 01-30-2006 02:35 AM
Make sed ignore lines Scarlos Shell Programming and Scripting 2 07-21-2005 10:33 AM
Removing duplicate lines ignore case hellsd UNIX for Dummies Questions & Answers 17 12-02-2004 09:47 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-18-2005
Registered User
 

Join Date: Feb 2004
Posts: 91
Ignore Lines Begining With #

Is there a standard way to make a shell script read a file, or list, and skip each line that contains # at the begining, or ignores the content starting after a # in line?

I'm looking to mimic the way commenting in a shell script normally works. This way I can comment my text files and lists my scripts process and ignore comment lines.

Thanks guys.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-18-2005
Registered User
 

Join Date: Jul 2005
Posts: 33
If you want to do only a couple things with the output, filter you source file in a pipeline:
Code:
grep -v '^[[:space:]]*#' /path/to/your/file | your_commands
if you want do extensive stuff with your comment-stripped file, save the stripped file in a temp file, and use use the temp file for the rest of the procedure:
Code:
grep -v '^[[:space:]]*#' /path/to/your/file  >/tmp/stripped_source
Note that I use ^[[:space:]]* at the start of the regular expression because lines that are completely comments can still contain leading whitespace. If you want to eliminate blank lines also, do
Code:
 egrep -v '(^[[:space:]]*#|^[[:space:]]*$)' /path/to/file >/tmp/stripped_file

Last edited by hadarot; 08-18-2005 at 08:53 PM..
Reply With Quote
  #3 (permalink)  
Old 08-18-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,374
Try...
Code:
$ cat file1
#line1
  #line2
line#3
line4

$ sed '/^ *#/d;s/#.*//' file1
line
line4
Reply With Quote
  #4 (permalink)  
Old 08-22-2005
Registered User
 

Join Date: Feb 2004
Posts: 91
Quote:
Originally Posted by Ygor
Try...
Code:
$ cat file1
#line1
  #line2
line#3
line4

$ sed '/^ *#/d;s/#.*//' file1
line
line4
After some testing I went with this method. Thanks to both of you.
Reply With Quote
  #5 (permalink)  
Old 08-23-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,471
If you want your script to behave like the ksh itself (ignore the part of a line after a "#" but use the part before it) you could do the following (replace "<spc>" with a literal space, "<tab>" with a tab char):

Code:
script

sed 's/#.*$/;s/^[<spc><tab>]*//;s/[<spc><tab>]*$//;/^$/d' file

content of file
# this is a line with comments
   # this too, but starting with blanks
command 1        # this line contains an inline comment

command 2 "#"   # this too, but my script would be confused

result
command 1
command 2 "
Alas, the script fails on the second line, but save for such delicacies it works.

bakunin
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 08:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66