Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Posts
    34

    Unanswered: 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.

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Code:
    perl -n -e "/Number=(\d+)[,}]/ and printf \"%d\n\",$1;" < myfile
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    Jul 2003
    Posts
    34
    Sorry...nothing returned by the above command.

  4. #4
    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> |
    +-----------------------+

  5. #5
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  6. #6
    Join Date
    May 2007
    Location
    Milano, Italy
    Posts
    22
    If you have GNU Awk:

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •