1

I'm trying to match up the data from an array the lists the days of the week like this:

MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY 

The files data reads like this:

Name1,Tuesday Name2,Friday Name3,Sunday Name4,Wednesday Name5,Thursday Name6,Saturday 

The output should be:

MONDAY TUESDAY Name1 WEDNESDAY Name4 THURSDAY Name5 FRIDAY Name2 SATURDAY Name6 SUNDAY Name3 

I am able to cut the data by the comma into two. I was wondering if it's possible to match the data from the file to the data within the array.

0

    1 Answer 1

    1

    If file1 has the days of the week and file2 has the days and names, then:

    $ awk -F, 'FNR==NR{a[toupper($2)]=$1;next} {print $1,a[$1]}' file2 file1 MONDAY TUESDAY Name1 WEDNESDAY Name4 THURSDAY Name5 FRIDAY Name2 SATURDAY Name6 

    How it works

    • -F,

      Use a comma as the field separator.

    • FNR==NR{a[toupper($2)]=$1;next}

      While we are reading through the first named file, file2, add an entry to associative array a with the key as the upper case version of the day and value as the name. Then, skip the rest of the commands and jump to the next line.

    • print $1,a[$1]

      If we get to here, we are working on the second file. In that case, print the day and the name that we retrieve from associative array a.

    1
    • If I were to add a corresponding set of data with phone numbers to that awk command, how would I go about it? For example: Name1,5551112222 Name2,5552223333 Name3,5554445555, etc.CommentedNov 23, 2015 at 1:19

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.