You must be using a really old shell since the newer Korn and POSIX shells have the coprocess feature built into them.
samos, what's the OS you're working on?
CentOS
could just anyone explain me how to store the variable a in file open by exec? because there is a function paste which do everything i need but only with files...all would be solved if i can put variables somehow in the files (which a canot create) and then use paste...
The paste command works with std. input as well.
Try this one out...
echo "$var1"
var1line1
var1line2
var1line3
var1line4
echo "$var2"
var2line1
var2line2
var2line3
echo "$var1" | awk '{print $1;print a}' a="$var2" | paste - - - - | awk '{print $1, $(NR+1)}'
var1line1 var2line1
var1line2 var2line2
var1line3 var2line3
var1line4
Or, as I already stated, with process substitution if your shell supports it:
$ printf '%s\n' "$v1"
var1line1
var1line2
var1line3
var1line4
$ printf '%s\n' "$v2"
var2line1
var2line2
var2line3
$ paste <(printf '%s' "$v1") <(printf '%s' "$v2")
var1line1 var2line1
var1line2 var2line2
var1line3 var2line3
var1line4
$
Assuming default CentOS installation the code above will work with bash.
yout code is working but only for lines without spaces...
e.g. var1='asd asd
fasd asd'
var2='das asd
f a '
result is:
asd das
fasd f
it concatenate only first words before space...
to radulov:
your code is notworking with print but with echo it worked but only in command line.
when i put it into script error occures with: unexpected token `('
I don't use print in my example, but printf. If you want that command to work in a script, you should use the bash shell:
bash <your_script>
or point the shebang to the correct interpreter:
#!/bin/bash
TIMTOWTDI
echo "$var1"
v1-1
v1-2
v1-3
v1-4
v1-5
v1-6
v1-7
echo "$var2"
v2-1
v2-2
v2-3
v2-4
v2-5
v2-6
v2-7
v2-8
v2-9
echo "$var1\n$var2" | \
awk '{
if (NR <= a)
x[NR] = $0
else
print x[++i], $0
} END {
for (i++; x; i++)
print x
}' a=$(echo "$var1" | wc -l)
Yep:
awk 'BEGIN { while ( "unix.com" ) {
getline _ < ARGV[1] || (_ = " ") && ++no
getline < ARGV[2] || ($0 = " ") && ++more
if (no && more) break
print _, $0 }
}' <(printf '%s' "$v1") <(printf '%s' "$v2")
For example:
% print $v1
v1-1
v1-2
v1-3
v1-4
v1-5
v1-6
v1-7
% print $v2
v2-1
v2-2
v2-3
v2-4
v2-5
v2-6
v2-7
v2-8
v2-9
% awk 'BEGIN {
while ( 42 ) {
getline _ < ARGV[1] || (_ = " ") && ++no
getline < ARGV[2] || ($0 = " ") && ++more
if (no && more) break
print _, $0
}
}' <(printf '%s' "$v1") <(printf '%s' "$v2")
v1-1 v2-1
v1-2 v2-2
v1-3 v2-3
v1-4 v2-4
v1-5 v2-5
v1-6 v2-6
v1-7 v2-7
v2-8
v2-9
% awk 'BEGIN {
while ( -9 ) {
getline _ < ARGV[1] || (_ = " ") && ++no
getline < ARGV[2] || ($0 = " ") && ++more
if (no && more) break
print _, $0
}
}' <(printf '%s' "$v2") <(printf '%s' "$v1")
v2-1 v1-1
v2-2 v1-2
v2-3 v1-3
v2-4 v1-4
v2-5 v1-5
v2-6 v1-6
v2-7 v1-7
v2-8
v2-9
Ok, try this new version, it must work much faster than the precedent version
#!/bin/ksh
IFS='
'
FINAL_RESULT=""
VARIABLE1="var1 first line
var1 second line
var1 third line
var1 forth line
var1 fifth line"
VARIABLE2="var2 first line
var2 second line
var2 third line
var2 forth line
var2 fifth line"
CURRENT_LINE_NUMBER=1
for ITERATOR in $VARIABLE1
do
CURRENT_LINE_IN_VARIABLE2=$(print "$VARIABLE2"\
|head -n $CURRENT_LINE_NUMBER|tail -n 1)
(( CURRENT_LINE_NUMBER = CURRENT_LINE_NUMBER + 1 ))
FINAL_RESULT="$FINAL_RESULT\n$ITERATOR$CURRENT_LINE_IN_VARIABLE2"
done
(( FINAL_LENGTH = ${#FINAL_RESULT} - 2 ))
FINAL_RESULT=${FINAL_RESULT:2:FINAL_LENGTH}
print "$FINAL_RESULT"