Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does grep -v '^#' do

Tags:

shell

My program looks like this.

ALL=`cat $1 | grep -v '^#' | wc -l`
FINISHED="0"

for i in `cat $1 | grep -v '^#'`; do
        echo "PROBE $i"
 I will be doing some operation
FINISHED=`echo $FINISHED"+1"|bc`

I will run this script by giving a file name as parameter where a list of probes will be present.

I have 2 questions

  1. What does grep -v '^#' mean. I learnt that '^ is usually used to matching a particular string. But in the file name which I give there is no #. Moreover I am getting the total number of probes for cat $1 | grep -v '^#' | wc -l.

  2. echo $FINISHED"+1"|bc. Here any idea as to why the developer as added |bc?

like image 811
user1107731 Avatar asked Mar 31 '13 06:03

user1107731


People also ask

What is grep is used for?

The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.

Does grep stand for anything?

In the simplest terms, grep (global regular expression print) is a small family of commands that search input files for a search string, and print the lines that match it.

What does grep do again?

grep searches input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or produces whatever other sort of output you have requested with options.

How do I grep a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.


2 Answers

  • ^ means "start of line"
  • # is the literal character #
  • -v means "invert the match" in grep, in other words, return all non matching lines.

Put those together, and your expression is "select all lines that do not begin with #"

| is the pipe character, it takes the output of the command on the left hand side, and uses it as the input of the command on the right hand side. bc is like a command line calculator (to do basic math).

like image 131
Burhan Khalid Avatar answered Oct 11 '22 14:10

Burhan Khalid


I would use this to exclude comments from the code I'm reading. So all comment lines start with # and I don't want to see them if there are too many of them.

grep -v '^#'
like image 25
Sony Mitto Avatar answered Oct 11 '22 12:10

Sony Mitto