Ignore Lines Begining With #


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore Lines Begining With #
# 1  
Old 08-18-2005
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. Smilie
# 2  
Old 08-18-2005
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 09:53 PM..
# 3  
Old 08-18-2005
Try...
Code:
$ cat file1
#line1
  #line2
line#3
line4

$ sed '/^ *#/d;s/#.*//' file1
line
line4

# 4  
Old 08-22-2005
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. Smilie
# 5  
Old 08-23-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Adding variable value in the begining of all lines present in a file.

I am getting the varible value from a grep command as: var=$(grep "Group" File1.txt | sed 's/Group Name*//g;s/,//g;s/://g;s/-//g') which leaves me the value of $var=xyz. now i want to append $var value in the begining of all the lines present in the file. Can u please suggest? Input file: 1... (10 Replies)
Discussion started by: rade777
10 Replies

2. Shell Programming and Scripting

Ignore lines in Shell Script

Hi, I have a shell script, which reads a *.txt file - line by line. In this text file, I have some lines beginning with "#" that I want to ignore : MY_FILE #blah blah blah 1 blah blah blah 2 blah blah blah 3 #blah blah blah 4 I want my script to read only the following lines... (3 Replies)
Discussion started by: ad23
3 Replies

3. Shell Programming and Scripting

ignore blank lines in while loop

Hi, i am having one text file it contains some blank lines and i want to ignore that blank lines . #! /bin/bash clear rdCount=0; while read myline do echo $myline let rdCount=$rdCount+1 done < ps.txt echo "Total line=$rdCount" and ps .txt contains the data- (17 Replies)
Discussion started by: aish11
17 Replies

4. Shell Programming and Scripting

Appending number of lines in the file to begining of the file

I am having 6 files named file1,file2....file6 and i need to append number of lines in each file to begining of the file. For example, If file 1 contains a b c d then after adding new line file1 should contain 4 a b c d Thanks in advance. (2 Replies)
Discussion started by: akhay_ms
2 Replies

5. Shell Programming and Scripting

sed command : print lines having no # at the begining

I am trying to print those line which has no # in the begining of the line. The sed I used for this purpose as shown below is not giving the required output. echo 'PDE 5600' | sed -n 's/^\!#/&/p' Where lies the problem:confused: (3 Replies)
Discussion started by: hiten.r.chauhan
3 Replies

6. Shell Programming and Scripting

expect - How to ignore empty lines?

Hi all, I'm looking for a way to generate an error when a command does not print an expected message. For example : test.sh : echo hi!test.exp : exp_internal 1 spawn ./test.sh expect { "hi!" {puts "bingo!"} "*" {puts "error!" ; exit 1} } I expected test.exp to match the string... (2 Replies)
Discussion started by: whbos
2 Replies

7. Shell Programming and Scripting

Ignore identical lines

Hello Experts, I have two files called "old" and "new". My old file contains 10 lines and my new file contains 10 + "n" lines. The first field in both these files contain ID. I sort these two files on ID. I am interested in only the lines that are in the new file and not in old. I tried... (4 Replies)
Discussion started by: forumthreads
4 Replies

8. Shell Programming and Scripting

awk, ignore first x number of lines.

Is there a way to tell awk to ignore the first 11 lines of a file?? example, I have a csv file with all the heading information in the first lines. I want to split the file into 5-6 different files but I want to retain the the first 11 lines of the file. As it is now I run this command: ... (8 Replies)
Discussion started by: trey85stang
8 Replies

9. Shell Programming and Scripting

How can I ignore only the lines which have # at the begining?

From the below file I want to grep only the lines except the comment sections. But grep -v "#" is eliminating the last line because it has one # in between. Any idea how can I ignore only the lines which have # at the begining (I mean comment lines) ? Thanks a lot to all in advance C Saha (1 Reply)
Discussion started by: csaha
1 Replies

10. Shell Programming and Scripting

Make sed ignore lines

Hi I use sed in a script for severall changes in files. I whish one of the substitutions I made to be aplied to every line that has the word "scripts" with the exception for the ones that start with "rsh", wich I wish sed to ignore . Is this possible? If yes, how can I do it? The substitution... (2 Replies)
Discussion started by: Scarlos
2 Replies
Login or Register to Ask a Question