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.
One could probably do it all in bash, but I suggest paste as the main command, as in:
Code:
#!/bin/bash -
# @(#) s1 Demonstrate paste, awk.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) paste awk
set -o nounset
echo
FILE1=data1
FILE2=data2
echo " Data file $FILE1:"
cat $FILE1
echo
echo " Data file $FILE2:"
cat $FILE2
echo
echo " Results:"
paste -d'#' $FILE1 $FILE2 |
awk '{ print NR "#" $0 }'
exit 0
Producing:
Code:
$ ./s1
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
paste (coreutils) 5.2.1
GNU Awk 3.1.4
Data file data1:
hello I'm
My name is
Data file data2:
David and you?
Pepe, what it's
Results:
1#hello I'm#David and you?
2#My name is#Pepe, what it's
There are an almost unlimited number of ways to do this. Since you asked "in bash", here's a pure bash solution which doesn't rely on external programs at all. For almost all practical purposes this is not going to be the best method, but it's instructive about how the shell handles files.
Code:
#!/bin/bash
end=0
while [ $end -eq 0 ]; do
Aend=0
Bend=0
read -u3 fileAline || Aend=1
read -u4 fileBline || Bend=1
if [ $Aend -eq 1 ] && [ $Bend -eq 1 ]; then
end=1
else
echo "#$fileAline#$fileBline#"
fi
done 3< fileA 4< fileB > fileC
Of course there are a great many variations of this. The version above is what spung to mind.
Specific things to note:
You can re-direct multiple files into a while loop using the "n<" syntax, where n is an integer file descriptor. 0, 1 and 2 are already used by the shell for stdin, stdout and stderr, so start your custom file descriptors at 3.
You can read some a numbered file descriptor using the -u option to the read command.
By putting || between two commands, the second one will only be executed if the return value of the first command is non-0. In the case of the read statements, read returns non-0 if the specified file descriptor is at the end of the file. This is documented in the bash manual page.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.