LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2007, 02:39 AM   #1
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Rep: Reputation: 15
Grep command to read a file


Hi,

i have a file of the form whose records are of the form

Name,gender,Address,Age


i want to write a script where in i want to check whether there are any individuals whose age>50
and how many such records are there??
 
Old 02-02-2007, 02:52 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
Code:
gawk -F, 'BEGIN {count=0} {if ($4 > 50) count++ } END {print count}' filename
will print the total number of people older than 50. If you want to print also their Name, you can try
Code:
gawk -F, 'BEGIN {count=0} {if ($4 > 50) {count++ ; print $1}} END {print count}' filename
or to print on one line
Code:
gawk -F, 'BEGIN {count=0} {if ($4 > 50) {count++ ; people[count]=$1}} END {printf "%d ",count ; for (i in people) printf "%s ",people[i]; printf "\n"}'

Last edited by colucix; 02-02-2007 at 04:45 AM.
 
Old 02-02-2007, 04:18 AM   #3
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Thanks colucix!!!

I will try it...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
grep command sgarci Linux - General 3 08-28-2006 07:13 AM
grep command in c????? alnreddy Linux - Software 1 12-30-2004 01:01 AM
tar command : error: file changed as we read it sneezesnoeze Linux - General 2 04-05-2004 06:56 PM
Read contents of file and execute command cosiek Programming 3 09-30-2003 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration