If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
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, 10: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, 10:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,454
Code:
perl -n -e "/Number=(\d+)[,}]/ and printf \"%d\n\",$1;" < myfile
Reply With Quote
  #3 (permalink)  
Old 12-24-07, 11: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, 09:00
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
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, 10:55
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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, 13: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

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