Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
bash: question about grep
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
lihao0129@gmail.com  
View profile  
 More options Jul 21 2008, 2:21 pm
Newsgroups: comp.unix.shell
From: "lihao0...@gmail.com" <lihao0...@gmail.com>
Date: Mon, 21 Jul 2008 11:21:05 -0700 (PDT)
Local: Mon, Jul 21 2008 2:21 pm
Subject: bash: question about grep
For the following TAB-delimited records, I want to count number of
records with column-2 == -1   (should be 2)

===== file.txt ======
AAA    -1    2008-07-14
BBB    -14   2008-07-15
CCC    -20   2008-07-16
DDD    -1    2008-07-16
===========

I tried:

  grep -c -- "-1\t" file.txt

which is not working. I know I can do it with awk, like:

 awk -F'\t' '$2==-1{t++}END{print t}' file.txt

But I would rather find a `grep` solution, any thought?? many thanks,

lihao


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephane CHAZELAS  
View profile  
(1 user)  More options Jul 21 2008, 2:48 pm
Newsgroups: comp.unix.shell
From: Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
Date: Mon, 21 Jul 2008 18:48:37 +0000 (UTC)
Local: Mon, Jul 21 2008 2:48 pm
Subject: Re: bash: question about grep
2008-07-21, 11:21(-07), lihao0...@gmail.com:

> For the following TAB-delimited records, I want to count number of
> records with column-2 == -1   (should be 2)

> ===== file.txt ======
> AAA    -1    2008-07-14
> BBB    -14   2008-07-15
> CCC    -20   2008-07-16
> DDD    -1    2008-07-16
> ===========

> I tried:

>   grep -c -- "-1\t" file.txt

grep -c -- $'-1\t' file.txt

$'...' is ksh93/zsh/bash specific.

> which is not working. I know I can do it with awk, like:

>  awk -F'\t' '$2==-1{t++}END{print t}' file.txt

> But I would rather find a `grep` solution, any thought?? many thanks,

[...]

Why, the awk solution looks a lot neater to me as closer to your
requirements.

Another one:

cut -f2 < file.txt | grep -cFxe -1

--
Stéphane


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lihao0129@gmail.com  
View profile  
 More options Jul 21 2008, 3:00 pm
Newsgroups: comp.unix.shell
From: "lihao0...@gmail.com" <lihao0...@gmail.com>
Date: Mon, 21 Jul 2008 12:00:40 -0700 (PDT)
Local: Mon, Jul 21 2008 3:00 pm
Subject: Re: bash: question about grep
On Jul 21, 2:48 pm, Stephane CHAZELAS <stephane_chaze...@yahoo.fr>
wrote:

wow, this is impressive. do you mind to explain what exactly is the
different between $'...' and '...'. I've never seen this $'..' thing
before. :-) many thanks

> > which is not working. I know I can do it with awk, like:

> >  awk -F'\t' '$2==-1{t++}END{print t}' file.txt

> > But I would rather find a `grep` solution, any thought?? many thanks,

> [...]

> Why, the awk solution looks a lot neater to me as closer to your
> requirements.

yes, just want to ask 'WHY' that grep line is not working...

> Another one:

> cut -f2 < file.txt | grep -cFxe -1

nice solution, :-)

thanks again,
lihao


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google