Display lines not starts with #


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Display lines not starts with #
# 1  
Old 03-12-2010
Display lines not starts with #

hiiiii

$ grep ^"#" $file

Will give the lines , which starts with # .And I wanna get the lines which are not starting with #.
How to implement that.

Thanking you
KrishSmilie
# 2  
Old 03-12-2010
Code:
grep ^[^#] file

You try this.
Here the ^ inside the character class is similar to neglecting the # character.

---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ----------

Using sed you can also achieve this as following way.
Code:
sed '/^#/d' lines


Last edited by thillai_selvan; 03-12-2010 at 06:15 AM..
This User Gave Thanks to thillai_selvan For This Post:
# 3  
Old 03-12-2010
Code:
grep -v ^# file

# 4  
Old 03-12-2010
If you put the ^ symbol inside the character class, it will not negate the pattern. It means , match the patter starting with. So the following code is not at all work.

Code:
grep [^#] file

Instead of that you should use the following code
Code:
grep ^[^#] file

Here the symbol ^ outside of the character class negate the pattern. The symbol ^ Inside character class represent the starting.
# 5  
Old 03-12-2010
Actually, I think it should be:
Code:
grep "^[^#]*" file

otherwise you will filter out empty lines too
# 6  
Old 03-12-2010
Quote:
Originally Posted by Scrutinizer
Actually, I think it should be:
Code:
grep "^[^#]*" file

otherwise you will filter out empty lines too

No this is totally wrong.
It will match the lines that begins with #.
* matches the zero or more occurrence of the previous character.
So it will take it as "other than beginning with hash" it will consider as zero occurrence and will
match the lines that begins with #.

Last edited by thillai_selvan; 03-12-2010 at 06:59 AM..
# 7  
Old 03-12-2010
Here's a comparison of the various methods proposed so far, with line numbers where possible:
Code:
$ awk '{print NR,$0}' test.txt # Not a solution, but to show the original file
1 This is a line
2 This is another line
3
4 The previous line is empty
5
6 The previous line contains whitespaces
7 # This shouldn't show
8 This should show again
$ grep -n ^[^#] test.txt
1:This is a line
2:This is another line
4:The previous line is empty
5:
6:The previous line contains whitespaces
8:This should show again
$ sed '/^#/d' test.txt
This is a line
This is another line

The previous line is empty

The previous line contains whitespaces
This should show again
$ grep -nv ^# test.txt
1:This is a line
2:This is another line
3:
4:The previous line is empty
5:
6:The previous line contains whitespaces
8:This should show again
$ grep -n "^[^#]*" test.txt
1:This is a line
2:This is another line
3:
4:The previous line is empty
5:
6:The previous line contains whitespaces
7:# This shouldn't show
8:This should show again

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to write a shell script that starts one, then kills it, then starts another?

This is on a CentOS box, I have two scripts that need to run in order. I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours (that I specify of course), and then calls the second script (they can't run simultaneously) which... (3 Replies)
Discussion started by: btramer
3 Replies

2. Shell Programming and Scripting

File lines starts with # not processed or exclude that lines from processing

I have a file like below #Fields section bald 1234 2345 456 222 abcs dddd dddd ssss mmmm mmm mmm i need do not process a files stating with # I was written code below while read -r line do if then echo ${line} >> elif then ... (3 Replies)
Discussion started by: Chenchireddy
3 Replies

3. Shell Programming and Scripting

File lines starts with # not processed or exclude that lines

I have requirement in my every files starting lines have # needs to be not processing or exclude the that lines. I have written a code like below, but now working as expected getting ERROR" line 60: 1 #!/bin/sh 2 echo ======= LogManageri start ========== 3 4 #This directory is... (1 Reply)
Discussion started by: Chenchireddy
1 Replies

4. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

5. Shell Programming and Scripting

Execution problem ---to remove the lines which starts with one type of character

Hi, I have one file, I need to check if file exist or not and then remove the lines which starts with ? My file1.out data is some thing abcabcppp xyzxyzpqr ????????? ????????? Output should be in test.out abcabcppp xyzxyzpqr I am getting the output as below but the File does not exist... (4 Replies)
Discussion started by: Ramyajiguru1
4 Replies

6. Shell Programming and Scripting

How to copy lines that starts with either 3 or 4 into new file?

Hi Guys, I have an awk script that would search the input file for line that starts with a number 3 and copies into a new text file. I want to extend this script to find the lines that either starts with 3 or a or b and copy all those lines into the new file. Here is what I have so far:... (1 Reply)
Discussion started by: Amith821
1 Replies

7. UNIX for Dummies Questions & Answers

Display last few lines

I have huge text files and I only want to display on the screen the last lines. with less -G file.txt i get the beginning of the file. (2 Replies)
Discussion started by: FelipeAd
2 Replies

8. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

9. Shell Programming and Scripting

Delete lines that starts with a certain letter

How can I delete those lines that starts with a certain letter? abc def ghi xyz abc def ace gik moq abe imq gxm I want to delete the line that starts with "x". Thanks! (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. Shell Programming and Scripting

display all lines

Dear Experts, can any one tell me how to display all lines except the last line of a file using awk. take care Regards, SHARY (3 Replies)
Discussion started by: shary
3 Replies
Login or Register to Ask a Question