Operating System - Linux
1753624 Members
5651 Online
108797 Solutions
New Discussion юеВ

Re: need a script to assign

 
SOLVED
Go to solution
IT_2007
Honored Contributor

need a script to assign

File1 has:

ABC
DCF
GRF

File2 has:

4
7
8


Want output should look like:

ABC 4
DCF 7
GRF 8

thanks.
11 REPLIES 11
Patrick Wallek
Honored Contributor
Solution

Re: need a script to assign

Use the 'paste' command.

# man paste

for more info.

Example:


# cat file1
abc
def
ghi

# cat file2
4
5
6

# paste file1 file2 > file3

# cat file3
abc 4
def 5
ghi 6
Peter Nikitka
Honored Contributor

Re: need a script to assign

Hi,

note, that the default delimiter of 'paste' is TAB character - use
paste -d' ' file1 file2 ...

to get 'real' blank.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
IT_2007
Honored Contributor

Re: need a script to assign

May be didn't explain well. Need to pass these variables in a command.

like:

command -H -I

so that it will take 1st variable from 1st file and 1st variable in 2nd file to execute command.

thanks.
A. Clay Stephenson
Acclaimed Contributor

Re: need a script to assign

I agree; you didn't explain very well.

#!/usr/bin/sh

paste file1 file2 | while read A B
do
command -H ${A} -I ${B}
done

If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: need a script to assign

>May be didn't explain well. Need to pass these variables in a command.

>like:

>command -H -I

>so that it will take 1st variable from 1st file and 1st variable in 2nd file to >execute command.

Try the shell script below which uses the coprocess functionality of sh-posix(1):
==============================================================

#!/usr/bin/sh

cat file1 |&

exec 3>&p; exec 4<&p

cat file2 |&

exec 5>&p; exec 6<&p

while true
do
read -u4 file1var1 && read -u6 file2var1

if [[ -z $file1var1 || -z $file2var1 ]]; then
break
else
echo "command -H $file1var1 -I $file2var1"
fi

done
Peter Nikitka
Honored Contributor

Re: need a script to assign

Hi,

a general approach for this type of problem is to handle with arrays:

In ksh syntax:
set -A A 'aa bb cc'
set -A B '11 22 33'
typeset -i i=-1
while [ i+=1 -lt ${#A[*]} ]
do print $i: ${A[i]} ${B[i]}
done

will produce
0: aa 11
1: bb 22
2: cc 33

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: need a script to assign


Here is a one-liner with awk.
It uses the main loop to read file 1 into $0.
Then for every loop it reads a line from file2 into a variable to be passde with $0 to the command (here: echo)

#awk '{getline f2 < "file2"; system ("echo " $0 " " f2)}' file1
abc 4
def 5
ghi 6


Here is an array style solution with perl.
It sucks up each file into an array, chops of teh newlines, calls teh comamdn for each entry in the first array, passing both:

perl -e 'open F,shift;@f1=; open F,shift;@f2=; chomp @f1; chomp @f2; for $i (0..@f1){ system("echo $f1[$i] $f2[$i]") }' file1 file2
abc 4
def 5
ghi 6

That's of course less ugly with a help file:

perl x.pl file1 file2
abc 4
def 5
ghi 6
------------- x.pl ----------
open F,shift;
@f1=;
open F,shift;
@f2=;
chomp @f1;
chomp @f2;
for $i (0..@f1){
system("echo $f1[$i] $f2[$i]")
}



IT_2007
Honored Contributor

Re: need a script to assign

Thanks to all for taking some time to explain.

Sandman,

Would you please explain it. I liked this way.

Thanks.
Dennis Handly
Acclaimed Contributor

Re: need a script to assign

>Would you please explain it. I liked this way.

Hmm, I've always thought that |& was useless junk.

If you look at ksh(1), you see |& under Commands.

<&p and >&p under Input/Output. And later it talks about redirecting them because you want another coprocess.

Then under read, you can read -u#.

So this does:
cat file1 |&
exec 3>&p; exec 4<&p

Puts cat in the background and the exec changes the output to cat to 3 and input from cat to 4. (3 seems pretty useless since cat is only reading from file1.)
exec with no executable just does file redirection/closing.

cat file2 |&
exec 5>&p; exec 6<&p

Again the same for 5 & 6. Note 4 and 6 are the output FROM the two cats.

read -u4 file1var1 && read -u6 file2var1

This reads one line from file 4 (from cat file1) and one line from file 6 (from cat file2).

if [[ -z $file1var1 || -z $file2var1 ]]; then

Checks for mismatched number of records and probably could print an error.