Looping through 2 files simultaneously

Hi all,

I'm having a problem with a script which should ultimately provide a filename by reading a value from file1 and file2 then join together.

I'm planning to use a loop/ loops to get the values out of both files and create a single string unfortunately the code currently treats the second loop (which is how I have written it to be fair) as nested instead of working simultaneously. The thing is that my sanity is slowly slipping away so I have stripped the code back to try and get a clearer picture. :wink:

The file's contain:

num.txt

65
65
66
67

first.txt

MRO0122
MRO0112
MRO0112
MRO0113

Currently the code returns:

MRO011365
MRO011365
MRO011366
MRO011367

What I want is

MRO012265
MRO011265
MRO011266
MRO011367

Anyway here's the code:

for update in `cat num.txt`
do
echo $update
for filename1 in `cat first.txt`
do
echo $filename1
truefile=$filename1$update.TXT
done

echo $truefile

done

Let me know if this is post is confusing in it's description and i'll try to provide clearer detail.

Any help will be thoroughly appreciated

I'm off to shoot myself.

Thanks

Chris

You can use awk here:

awk 'NR==FNR{a[NR]=$0;next}{$0=$0 a[FNR]}1' num.txt first.txt

hi Chris.
did you consider using paste.
"paste" can paste the lines from the two files together.

 paste -d "" first.txt num.txt

bash

exec 4<"file1"
while read -r line
do
    read -r s <&4
    echo "${line}${s}"
done <"file"
exec >&4-

# paste -d "" first.txt num.txt
paste: no delimiters specified

To be POSIX compliant.

# paste -d \0 first.txt num.txt
MRO0122065
MRO0112065
MRO0112066
MRO0113067

hello .
which unix are you using. It works on GNU/Linux though.
Anyways thanks for the info.
Regards

FreeBSD, but take a look at POSIX paste man page

yeah sure. but linux does have lots of differences from Freebsd as linux is not that much into POSIX :slight_smile: :slight_smile:
thanks for the post. I had a look .
Regards