Getting Files from list and jumbles them

Hi,

I was wondering if you could help as I'm sure its possible. I can remember something was possible using sed/awk a while back.

I have a list in a file such as sample_list.txt which contains the following:

example1, answer1
example2, answer2
example3, answer3
example4, answer4
example5, answer5
example6, answer6
example7, answer7
example8, answer8
example9, answer9
example10, answer10
e.t.c

What I want to do is to create a script and when it runs:

  1. It lists either the first or second field and places it on the left hand side
  2. It then places its corresponding example/answer on the opposite side
  3. It repeats this for 10 of the words and then shuffles the words

example:

example8	answer1
example10	answer6
example1	answer8
answer3	example5
example7	answer9
example9	answer7
answer2	answer9
example6	example3
example4	example2
answer5	answer4

Hope this helps.

Thanks

I do not understand the practical use for something that takes a random number of examples (let us call this random number of examples example_count ) where example_count is an integer such that 1 <= example_count <= 20 and then takes 20 - example_count answers to produce 10 lines of output some of which have two examples, some of which have two answers, some of which have an example followed by an answer, and the rest have an answer followed by an example.

I could imagine needing to extract 10 examples and the 10 answers corresponding to those examples and creating 10 lines with the examples on the left and the answers on the right (but with the answers in random order) or creating 10 lines with the answers on the left and the examples on the right (but with the examples in random order), but neither of these match the output you say you want.

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum with a completely filled out homework template that is available in the special homework rules.

If this is homework, please review the guidelines for posting homework and repost.

If it is not homework, please explain how this project will be used.

1 Like

Hi,

Thank you for getting back to me.

I can see why you think it's homework :slight_smile:

I would like you to know that it is not part of homework but is a process carried out by the local school teachers on a daily basis. To make life easier for them I suggested that it could be done using sone form of shell script and that is where I thought you guys came in.

Hopefully this clears and misunderstanding on my part.

Thanjs

Good you pointed that out.

Still I'm missing the point. What be

  • "its corresponding example/answer"?
  • "shuffles the words" and its effect?
    I can't even infer that from the samples given. Why does example8 correspond to answer1 ?

okay.

On a weekly basis each class is given synonyms to do.
These are generated from a synonyms file which contains the following

abandon, leave
abbreviate, shorten
abode, dwelling
abrupt, sudden
...
..
.

so the left hand side you would have the words

abandon
abbreviate
abode
abrupt

on the right hand side you will have

sudden
dwelling
leave
shorten

The order does not matter.

So as pupils get the sheet they have to draw lines to match the synonyms.
Hope that helps.

Unfortunately, you didn't really answer my questions. Based on some assumptions, I came up with

awk '
        {EX[NR]  = $1
         EX[-NR] = $2
        }
END     {for (i=1; i<=NR; i++)  {IX = 0
                                 IY = 0
                                 do     IX = int(rand()*20)-10
                                 while  (!(IX in EX))

                                 SGN = (IX < 0)?1:-1

                                 do     IY = SGN*(int(rand()*10)+1)
                                 while  (!(IY in EX) || IY == SGN*IX)

                                 printf "%-15s %-15s\n", EX[IX], EX[IY]
                                 delete EX[IX]
                                 delete EX[IY]
                                }
        }
' FS=, file
example7        answer10       
answer2         example6       
example2        answer5        
answer3         example10      
example9        answer1        
answer6         example4       
example8        answer9        
example5        answer8        
example3        answer4        
answer7         example1       

Does it come close to what you imagined?

This one would make you independent of the input row count; use it on files of arbitrary length...

awk '
        {EX[NR]  = $1
         EX[-NR] = $2
        }
END     {for (i=1; i<=NR; i++)  {IX = 0
                                 IY = 0
                                 do     IX = int(rand()*2*NR) - NR
                                 while  (!(IX in EX))

                                 SGN = (IX < 0)?1:-1

                                 do     IY = SGN*(int(rand()*NR)+1)
                                 while  (!(IY in EX) || IY == SGN*IX)

                                 printf "%15s      %-15s\n", EX[IX], EX[IY]
                                 delete EX[IX]
                                 delete EX[IY]
                                }
        }
' FS=, file
1 Like

Maybe a bit of Perl?

perl -MList::Util=shuffle -nalF',\s?' -e '
($o, $t)=shuffle @F;
push @x, $o; push @y, $t;

END{
    @x=shuffle @x;
    @y=shuffle @y;
    for(0 .. $#x){printf "%-15s %s\n", $x[$_], $y[$_]}
}' example.txt
answer2         answer5
example6        example2
example9        answer10
example7        answer7
example10       answer3
answer1         answer6
example4        example1
answer8         answer4
example3        answer9
example5        example8

How do I invoke the above script: Tried the following:

awk -f  rudic.awk sample_list.txt

You can run it as given exchanging "file" with your file's name. Or, put everything within but excluding the single quotes into the .awk file, and run like

awk -fxxx.awk FS=, yourfilename

.

Got the script to work :wink:

awk -f unix.awk sample.txt
        answer6      example3,
      example6,      answer2
      example1,      answer9
        answer7      example5,
        answer3      example10,
        answer5      example9,
      example2,      answer4
        answer8      example7,
       answer10      example8,
        answer1      example4,

But its got both the answer1 and example1 the same side.

What it is supposed to do is 1-10 of both the example and answer on either side.

Thanks for taking time to look at this.

Note that you should have used:

awk -f unix.awk FS=, sample.txt

(as RudiC suggested in post #10 in this thread) to get rid of the commas after all of the example#s in your output (and to allow the script to work when an example or answer contains more than one "word").

Please go back and look at your post #1 in this thread where you showed output exactly like that above with examples and answers in both columns of the desired output. Then look at post #2 where I said:

and in post #3 you stated:

which we all understood to be a clear statement that you wanted examples and answers mixed in the columns; not kept in separate columns.

So, now that you have changed your mind; please be very clear about what you want:

  1. Do you want examples in the left column of output and answers in the right column of output?
  2. Do you want answers in the left column of output and examples in the right column of output?
  3. Do you want the script to randomly decide whether to put examples or answers in the left column and answers or examples, respectively, in the right column?
  4. Do you want the script to interactively ask the user which field should be in the left column?
  5. Do you want the script to default to putting examples in the left column and answers in the right column and accept an option to switch the order?
  6. Or do you want the script to require an operand that specifies which data goes in the left column?

Please also tell us:

  1. The output you said you want uses a single tab character to separate output fields. Is that really what you want, or do you want both output columns aligned?
  2. The input shows a <comma> (or <comma><space>) as the input file field separator. What input field separator do you want to use? (Note that whatever field separator you choose to use will not be allowed to appear in any example and will not be allowed to appear in any answer!)
  3. What shell are you using?
  4. What operating system are you using?

Do you have access to Perl?
That problem is solved in my solution in post number 8
Just copy and paste on your command line, change the example.data for your real filename:

perl -MList::Util=shuffle -nalF',\s?' -e '
($o, $t)=shuffle @F;
push @x, $o; push @y, $t;

END{
    @x=shuffle @x;
    @y=shuffle @y;
    for(0 .. $#x){printf "%-15s %s\n", $x[$_], $y[$_]}
}' example.data

Thanks everyone for your efforts, but I'm not remotely convinced that "it is not part of homework but is a process carried out by the local school teachers on a daily basis", so the thread is closed.

1 Like