Unix Shell - shell script to get second field with specific range

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > shell script to get second field with specific range





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author shell script to get second field with specific range
tester

2006-02-21, 5:54 pm

Hi gurus,
I have file with follwoing output:
2738 sshah
2732 denton
2731 stark
161 carnason
160 strupeni
160 sthind
160 stephh
158 sonjac
158 rmorcom
158 jcurtis
158 gargs
156 tricias
155 sshakiro
155 mattdonn
101 test
112 test2

I need to have all the entries which ranges from 100 to 150 to a new
file with only second field.
like my new file shoudl contain only
test2
test

ia m trying this with awk. abut unable to make it work.
Any help is greatly appreciated.
Regards
b

joe@invalid.address

2006-02-21, 8:48 pm

"tester" <bshah@citadon.com> writes:

> Hi gurus,
> I have file with follwoing output:
> 2738 sshah
> 2732 denton
> 2731 stark
> 161 carnason
> 160 strupeni
> 160 sthind
> 160 stephh
> 158 sonjac
> 158 rmorcom
> 158 jcurtis
> 158 gargs
> 156 tricias
> 155 sshakiro
> 155 mattdonn
> 101 test
> 112 test2
>
> I need to have all the entries which ranges from 100 to 150 to a new
> file with only second field. like my new file shoudl contain only
> test2 test
>
> ia m trying this with awk. abut unable to make it work. Any help is
> greatly appreciated. Regards b


Just run it through sort before giving it to awk

sort -n data | awk '$1 <= 150 && $1 >= 100 {print $2}'

Joe
--
Gort, klatu barada nikto
Chris F.A. Johnson

2006-02-21, 8:48 pm

On 2006-02-21, tester wrote:
> Hi gurus,
> I have file with follwoing output:
> 2738 sshah
> 2732 denton
> 2731 stark
> 161 carnason
> 160 strupeni
> 160 sthind
> 160 stephh
> 158 sonjac
> 158 rmorcom
> 158 jcurtis
> 158 gargs
> 156 tricias
> 155 sshakiro
> 155 mattdonn
> 101 test
> 112 test2
>
> I need to have all the entries which ranges from 100 to 150 to a new
> file with only second field.
> like my new file shoudl contain only
> test2
> test
>
> ia m trying this with awk. abut unable to make it work.


awk '$1 >= 100 && $1 <= 150 { print $2 }' file > newfile


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
tester

2006-02-21, 8:48 pm


Chris F.A. Johnson wrote:
> On 2006-02-21, tester wrote:
>
> awk '$1 >= 100 && $1 <= 150 { print $2 }' file > newfile

Is there any way i can get the additiion (total) of col. 1 which were
seleted for the output...range 100-150.

Chris F.A. Johnson

2006-02-21, 8:48 pm

On 2006-02-22, tester wrote:
>
> Chris F.A. Johnson wrote:
>
> Is there any way i can get the additiion (total) of col. 1 which were
> seleted for the output...range 100-150.


awk '$1 >= 100 && $1 <= 150 { print $2 ; total += $1 }
END { print total > "/dev/tty" }' file > newfile

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Bruce Barnett

2006-02-21, 8:48 pm

joe@invalid.address writes:

> "tester" <bshah@citadon.com> writes:
>
> Just run it through sort before giving it to awk
>
> sort -n data | awk '$1 <= 150 && $1 >= 100 {print $2}'


That's a UUOS - Useless Use of Sort.


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
Bruce Barnett

2006-02-21, 8:48 pm

"tester" <bshah@citadon.com> writes:

[vbcol=seagreen]
> Is there any way i can get the additiion (total) of col. 1 which were
> seleted for the output...range 100-150.


awk '$1 >= 100 && $1 <= 150 { total+=$1;print $2} END{print "TOTAL " total}' <file >newfile


--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
tester

2006-02-21, 8:48 pm


Thanks Chris...That worked great...

> awk '$1 >= 100 && $1 <= 150 { print $2 ; total += $1 }
> END { print total > "/dev/tty" }' file > newfile


joe@invalid.address

2006-02-21, 8:48 pm

Bruce Barnett <spamhater113+U060221193033@grymoire.com> writes:

> joe@invalid.address writes:
>
>
> That's a UUOS - Useless Use of Sort.


Yeah, it's been a long day.

Joe
--
Gort, klatu barada nikto
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com