Mix two files line by line

I have a file "fruits" and one "colors"

File "colors" contains:

green
red

File "fruits" contains:

apple
banana

I want the output to show all possibilities of fruits and colors:

greenapple
greenbanana
redapple
redbanana

The order is not important, but I need all possibilites.

Any ideas?

What have you tried so far?
What language would you like to use?
What OS and version are you using?
Do you want a space between colours and fruit or not?

Mostly, what have you tried so far.

Sorry to be a little terse, but if you've tried something that you don't like, then tell us and it will stop us going the same way. You will get better responses for putting in some effort yourself first and explaining exactly where you are stuck.

It's easy to give you an answer (and there are lots of ways to get thing output you suggest), but then you will not be learning or we might give you something inappropriate to your other needs not listed.

Robin
Liverpool/Blackburn
UK

1 Like

I have no idea where to start.

I could do a big shellscript to do it, but I believe there's something much simpler that I'm missing.

Consider this logic:-

  • You want to read a file called colours
  • For each line it contains, you want to do the following
    [list]
  • You want to read a file called fruits
  • For each line, you want to display the current colour followed by the current fruit
    [/list]

If you are in a shell script (ksh, bash, etc.) you need to have a loop to read in each file similar to this:-

while read variable
do
   some action
done < file

Other coding options will exist for awk, sed, perl, csh and the list goes on. This is why it would be good to know what you are starting with this issue. What are you experienced in? That's normally a good way to think about things.

Is this homework perhaps? If so, there is a homework forum.

Does that help? Would you like to experiment and let us know how you get on?

Robin

2 Likes
Moderator comments were removed during original forum migration.
Moderator comments were removed during original forum migration.
1 Like

If you see that a member is donating of their valuable time to teach someone how to solve their problem -- a process which requires some contribution from the one being helped and which is more likely to result in learning -- please don't undermine that effort by providing a complete, ready-made solution.

Regards,
Alister

5 Likes

Sorry alister, Don Cragun, drl,Jim mcnamara and Scott. Since sub forum is Shell Programming and Scripting, I posted answer. If thread was in Homework & coursework Questions, I would not posted ready-made solution. Please forgive me.

Sometimes we DO need a ready made solution, and Akshay was much more helpful than anyone else in this thread.

It's amazing how some people think they know what's best for everyone.

Thank you Akshay.