Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Reading a particular string value from a file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-24-07, 11:34
asram asram is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Reading a particular string value from a file

Hi, Please help on this. I have several lines in a file in the following format:

{Name=abc,Number=423,CustNo=123}
{Name=abc,Number=890,CustNo=5665}
{Number=423,Name=efg,CustNo=7878}
{Name=abc,Number=423,CustNo=123}
{CustNo=7879,Name=abc,Number=423}
{CustNo=7879,Name=abc,Number=890}

Basically I want to pull all the unique "Number" field values irresepctive of other fields. It can be there anywhere in the line and with different values.
Appreciate your help.
Reply With Quote
  #2 (permalink)  
Old 12-24-07, 11:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 2,227
Code:
perl -n -e "/Number=(\d+)[,}]/ and printf \"%d\n\",$1;" < myfile
__________________
===
Nick Ivanov
Freelance database consultant
www.datori.org
Reply With Quote
  #3 (permalink)  
Old 12-24-07, 12:32
asram asram is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Sorry...nothing returned by the above command.
Reply With Quote
  #4 (permalink)  
Old 12-26-07, 10:00
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 324
nawk -f asram.awk myFile

asram.awk:
Code:
BEGIN { FS="[{}=,]" } { for(i=1; i<=NF; i++) if($i == "CustNo") a[$(i+1)] } END { for(i in a) print i }
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 12-26-07, 11:55
mike_bike_kite mike_bike_kite is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 955
Asram

You could also try
Code:
cat myfile | grep Number | sed 's/.*Number=//' | sed 's/[^0-9].*//' | sort | uniq

Which just keeps the lines with Number
Removes everything up to and including Number=
Removes anything after the number left over
Sorts and shows the unique values

Which produces
Code:
423 490

Mike
Reply With Quote
  #6 (permalink)  
Old 12-29-07, 14:01
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
If you have GNU Awk:

Code:
awk '!x[$2]++&&$0=$2' FS="Number=" RS="[,}]" data
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On