Results 1 to 5 of 5

Thread: Pattern Matching/Data Help

  1. #1
    Join Date
    Feb 2006
    Location
    San Antonio
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Pattern Matching/Data Help

    Hello all, I could use some advice on writing a script:

    I have two files.
    One looks like this:

    Grades.txt


    Mary 100
    John 90
    Doug 85

    The other looks like this:

    ID Numbers


    Mary 000111
    John 000222
    Doug 000333

    Now, I'm trying to figure out how to write a script using awk, sed, cut, or anything really that will produce output like this:

    000111 100
    000222 90
    000333 85

    I need to grab the name and score, read them into a variable, then compare the name variable against the id list and print id number, score.

    Any shell wizardry out there tonight?
    Thanks,
    Pros
    Josh Beck
    Northeast Independent School District
    KSAT (Krueger School of Applied Technologies)
    http://linuxclassroom.com

  2. #2
    Join Date
    Apr 2008
    Location
    Finland
    Beans
    173
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Pattern Matching/Data Help

    Nice little brainteaser for sundaymorning
    Here's a tryout with a very un-efficient algorithm

    Code:
    #!/bin/bash
    
    O=$IFS
    IFS=`echo -en "\n\b"`
    for fil in $(cat $1 | sort)
    do
    	name=$(echo $fil | sed 's:\(.*\) .*:\1:')
    	for filu in $(cat $2)
    	do
    		nam=$(echo $filu | sed 's:\(.*\) .*:\1:')
    		if [ "$nam" == "$name" ]
    		then
    			id=$(echo $filu | sed 's:.* \(.*\):\1:')
    			points=$(echo $fil | sed 's:.* \(.*\):\1:')
    			echo $id $points
    		fi
    	done
    done
    IFS=$O

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Pattern Matching/Data Help

    this is kind of a 'toy' solution, in that it works for the supplied data, but won't scale well for real-world data, but just for fun:
    Code:
    sort grades.txt numbers.txt | sed 'N;s/^\([^ ]*\) \([0-9]*\)\n\1/\2/'
    Code:
    xxxx@yyyyyy:~/tmp$ cat grades.txt
    Mary 100
    John 90
    Doug 85
    xxxx@yyyyyy:~/tmp$ cat numbers.txt
    Mary 000111
    John 000222
    Doug 000333
    xxxx@yyyyyy:~/tmp$ sort grades.txt numbers.txt | sed 'N;s/^\([^ ]*\) \([0-9]*\)\n\1/\2/'
    000333 85
    000222 90
    000111 100

  4. #4
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Pattern Matching/Data Help

    The following works with the sample data provided:

    Code:
    join file1 file2 | cut -d " " -f 2,3
    To illustrate:

    $ cat file1
    Mary 000111
    John 000222
    Doug 000333

    $ cat file2
    Mary 100
    John 90
    Doug 85

    $ join file1 file2 | cut -d " " -f 2,3
    000111 100
    000222 90
    000333 85
    An alternative, that sorts the data but is less like to break:

    Code:
    join <(sort file1) <(sort file2) | cut -d " " -f 2,3
    SOURCE REFERENCE
    http://superuser.com/questions/25302...ing-temp-files
    Last edited by kaibob; September 7th, 2009 at 02:52 AM.

  5. #5
    Join Date
    Feb 2006
    Location
    San Antonio
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Pattern Matching/Data Help

    Thanks for the solutions everyone. Wasn't familiar with some of that.
    TY
    Josh
    Josh Beck
    Northeast Independent School District
    KSAT (Krueger School of Applied Technologies)
    http://linuxclassroom.com

Bookmarks

Posting Permissions

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