Operating System - HP-UX
1753519 Members
3675 Online
108795 Solutions
New Discussion

Re: Concatenate using for

 
SOLVED
Go to solution
Fefers
Frequent Advisor

Concatenate using for

Hi...

 

How can I concatenate 2 files using for? (bash - shell script)

Example:

 

file1.txt

apple

banana

 

file2.txt

red

yellow

 

I wanna do this:

 

for FRUIT in file1.txt "AND" COLOR in file2.txt ; do

echo "The fruit $FRUIT is $COLOR ;

done

 

The fruit apple is red

The fruit banana is yellow

 

I don't know how to use this "AND".. To use 2 or more variables inside the for command..

 

could someone help me please?

 

Thanks

2 REPLIES 2
Patrick Wallek
Honored Contributor
Solution

Re: Concatenate using for

If your input file is sorted correct I would do the following:

 

paste file1.txt file2.txt >> file3.txt

while read FRUIT COLOR

do

echo "The ${FRUIT} is ${COLOR}"

done < file3.txt

Fefers
Frequent Advisor

Re: Concatenate using for

Hi Patrick..

 

It worked! Exactly what I needed.

 

 

Thanks.