ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/sh
cat >/tmp/jointest.1 <<DONE
A 1-A
B 1-B
C 1-C
D 1-D
DONE
cat >/tmp/jointest.2 <<DONE
A 2-A
C 2-C
E 2-E
F 2-F
DONE
join -a 1 -a 2 -j 1 -o 0,1.2,2.2 -e '###' -- /tmp/jointest.1 /tmp/jointest.2
Many thanks Daniel for the coming up with the scripts. I think i am almost there. I saved your script into script.sh and i edited your script to run my 12 files as follow:
Code:
# File identification
Path=$(cut -d'.' -f1 <<< ${0})
OutFile=$Path"out.txt"
filename1.txt=$Path"inp1.txt"
filename2.txt=$Path"inp2.txt"
filename3.txt=$Path"inp3.txt"
filename4.txt=$Path"inp4.txt"
filename5.txt=$Path"inp5.txt"
filename6.txt=$Path"inp6.txt"
filename7.txt=$Path"inp7.txt"
filename8.txt=$Path"inp8.txt"
filename9.txt=$Path"inp9.txt"
filename10.txt=$Path"inp10.txt"
filename11.txt=$Path"inp11.txt"
filename12.txt=$Path"inp12.txt"
Work1=$Path"w1.txt"
Work2=$Path"w2.txt"
Work3=$Path"w3.txt"
Work4=$Path"w4.txt"
Work5=$Path"w5.txt"
Work6=$Path"w6.txt"
Work7=$Path"w7.txt"
Work8=$Path"w8.txt"
Work9=$Path"w9.txt"
Work10=$Path"w10.txt"
Work11=$Path"w11.txt"
Work12=$Path"w12.txt"
Ustrs=$Path"ustrs.txt"
# Make a file of all unique strings (Ustrs)
sort $Path'inp'*'.txt' -u > $Ustrs
# nif = number of input files
nif=$(ls $Path'inp'*'.txt' | wc -l)
# Make working copies of each input file with
# blank lines inserted for "missing" words.
for (( j=1;j<=nif;j++ )) ;
do
sort $Path'inp'$j'.txt' $Ustrs \
|uniq -c \
|sed 's/ 1 .*/ /' \
|cut -c9- \
>$Path'w'$j'.txt'
done
# Paste all the work files together
# to form finished matrix.
paste $Path'w'*'.txt' >$OutFile
But the terminal showed me "Syntax error:redirection unexpected. What have i done wrongly in editing the script?
... the terminal showed me "Syntax error:redirection unexpected. What have i done wrongly in editing the script?
There is not enough information here to give an informed answer. I see nothing wrong with the changes you made.
1) Are all 12 input files and the script located in the same folder?
2) Have you tried to execute the script with only five input files?
3) Are the input files small enough to post here? If not, can they be put on a web page which I may access?
4) You said "I saved your script into script.sh" I don't know what that means. My program is named /home/daniel/Desktop/LQfiles/dbm678.bin
The entire program is given below...
Daniel B. Martin
Code:
#!/bin/bash Daniel B. Martin Mar13
#
# To execute this program, launch a terminal session and enter:
# bash /home/daniel/Desktop/LQfiles/dbm678.bin
#
# This program inspired by:
# http://www.linuxquestions.org/questions/programming-9/
# linux-join-command-for-multiple-files-4175454012/
# File identification
Path=$(cut -d'.' -f1 <<< ${0})
OutFile=$Path"out.txt"
InFile1=$Path"inp1.txt"
InFile2=$Path"inp2.txt"
InFile3=$Path"inp3.txt"
InFile4=$Path"inp4.txt"
InFile5=$Path"inp5.txt"
Work1=$Path"w1.txt"
Work2=$Path"w2.txt"
Work3=$Path"w3.txt"
Work4=$Path"w4.txt"
Work5=$Path"w5.txt"
Ustrs=$Path"ustrs.txt"
echo
echo "Method of LQ member danielbmartin #1"
# Make a file of all unique strings (Ustrs)
sort $Path'inp'*'.txt' -u > $Ustrs
# nif = number of input files
nif=$(ls $Path'inp'*'.txt' | wc -l)
# Make working copies of each input file with
# blank lines inserted for "missing" words.
for (( j=1;j<=nif;j++ )) ;
do
sort $Path'inp'$j'.txt' $Ustrs \
|uniq -c \
|sed 's/ 1 .*/ /' \
|cut -c9- \
>$Path'w'$j'.txt'
done
# Paste all the work files together
# to form the finished product.
paste $Path'w'*'.txt' >$OutFile
echo; echo "OutFile ..."; cat $OutFile
echo; echo "Normal end of job."; echo
exit
1) Are all 12 input files and the script located in the same folder?
2) Have you tried to execute the script with only five input files?
3) Are the input files small enough to post here? If not, can they be put on a web page which I may access?
4) You said "I saved your script into script.sh" I don't know what that means. My program is named
/home/daniel/Desktop/LQfiles/dbm678.bin
The entire program is given below...
Yes, i have checked through question 1 & 2, but i get the same results.
For question 3, i couldn't post my files up as it is confidential.
For question 4, i tried to say i save your program into a file with the filename of 'script.sh' after i edit the program as showed above.
When i run your program in the terminal i just key in:
Code:
sh script.sh
Anyway, really thank you for your help, Daniel. I will use other alternative to solve this problem.
Does your script reference /bin/bash or /bin/sh in its bash bang line? The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh then your script will be using a different shell than you expect. Dash does not have the <<< redirection operator.
Or do it the simpler,
Code:
Path=$(pwd)/
Or use the shell
Path=$PWD/
instead of
Path=$(cut -d'.' -f1 <<< ${0})
Last edited by whizje; 03-15-2013 at 03:33 AM.
Reason: typo
Does your script reference /bin/bash or /bin/sh in its bash bang line? The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh then your script will be using a different shell than you expect. Dash does not have the <<< redirection operator.
My bash is self-taught and admittedly weak, mostly picked up by copying code found on this forum.
I code #!/bin/bash in the first line of my bash programs without understanding the reason.
Pure mimicry.
This is the first line of my program dbm678.bin ...
# To execute this program, launch a terminal session and enter:
# bash /home/daniel/Desktop/LQfiles/dbm678.bin
When you run a script this way, the #! line is ignored as a comment. It would only be used if you did:
Code:
/home/daniel/Desktop/LQfiles/dbm678.bin
ie, execute the script directly without explicitly calling bash.
This also bypasses the #! line, since you invoked sh directly.
The first line in program dbm684 is #!/bin/bash Daniel B. Martin Mar13
When I enter bash /home/daniel/Desktop/LQfiles/dbm684.bin the program executes normally.
When I enter /home/daniel/Desktop/LQfiles/dbm684.bin the result is bash: /home/daniel/Desktop/LQfiles/dbm684.bin: Permission denied
It's unclear what benefit is obtained by the #!/bin/bash
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.