Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Here is a different view of the problem.
This may be conceptually simpler, but requires a feature of the bash shell that is not often used. Essentially, we cause lines to be read in sets, one from each data file. The third file is not a file, but a process -- i.e. a running program.:
Code:
#!/usr/bin/env bash
# @(#) s4 Demonstrate gathering, perfect shuffle, with paste.
echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) paste jot
set -o nounset
echo
FILE1=data1
FILE2=data2
echo
echo " Data file $FILE1:"
cat $FILE1
echo
echo " Data file $FILE2:"
cat $FILE2
echo
echo " Results:"
n=$( wc -l $FILE1 )
paste -d '\n' $FILE1 $FILE2 <( jot -b "===" $n )
exit 0
Producing:
Code:
% ./s4
Environment: LC_ALL = C, LANG = C
(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
jot - ( /usr/bin/jot Aug 18 2003 )
Data file data1:
Aaa
Bbb
Ccc
Ddd
Eee
Fff
Data file data2:
Zzz
Yyy
Xxx
www
vvv
uuu
Results:
Aaa
Zzz
===
Bbb
Yyy
===
Ccc
Xxx
===
Ddd
www
===
Eee
vvv
===
Fff
uuu
===
The drawbacks are that a shell with that specific feature is needed, the utility jot is needed, and a file must be read to get the line count. However, it may be easier than thinking about awk, perl, tcl, etc ... cheers, drl
Here's a way to do this using only shell with no external commands.
It's nowhere near the elegance of the other solutions posted here.
It's an example of how to use file descriptors to read more than one file at a time.
If both files have same number of lines...
Code:
#!/bin/sh
exec 3< file1.txt # Open file1.txt for input on file descriptor 3
exec 4< file2.txt # Open file2.txt for input on file descriptor 4
while : ; do
read <&3 line1 # read next line from file1.txt into line1
[ $? -ne 0 ] && break # If finished reading file1, break out of loop.
echo "$line1" # otherwise print the line.
read <&4 line2 # do the same for file2.txt
echo "$line2"
echo "-----"
done
3<&- # Close the file descriptors
4<&-
This will output everything to stdout. Can redirect to a file if desired.
If files have unequal number of lines, and we want to print all lines of both files, we need a bit more code.
This will print the contents of both files, with the ----- separator, until the shorter file is finished.
Then it just prints the rest of the longer file, with no ----- separator.
Code:
#!/bin/sh
exec 3< file1.txt # Open file1.txt for input on file descriptor 3
exec 4< file2.txt # Open file2.txt for input on file descriptor 4
F1=0 F2=0 # Flags indicating file has more to read, 0 = true in shell.
while [ $F1 -eq 0 -o $F2 -eq 0 ]; do # At least one file has more to read
read <&3 line1 # read file1
F1=$? # Gets set to non-zero if finished reading
[ $F1 -eq 0 ] && echo "$line1" # don't print if we didn't read something.
read <&4 line2
F2=$?
[ $F2 -eq 0 ] && echo "$line2"
[ $F1 -eq 0 -a $F2 -eq 0 ] && echo "-----" # Print if both files are still reading
done
3<&- # Close the file descriptors
4<&-
Probably a way to do this with fewer tests...Oh well...
Hi,
I have many files but with only 2 names , I want to load the data of that file into database through sqlldr with single control file. how can i do that ?????
Example:
switch_file
switch_file
billing_file
billing_file
now these files should be loaded into same database but different... (1 Reply)
Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file.
While reading and merging the file contents into a single file, Like to keep the format of the source file.
... (4 Replies)
hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file
<sequence>
<Filename>aldorzum.doc</Filename>
<DivisionCode>US</DivisionCode>
<ContentType>Template</ContentType>
<ProductCode>VIMZIM</ProductCode>
</sequence>... (1 Reply)
Hello.
I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back.
The problem is that the source and destination file has a lot of default: strings in it but with different values...
So..
Here is an example:
A part of my source... (6 Replies)
Hi,
I have a file with few values in it. I need script help to read file line by line and check:
1/if it's a file (with extension eg .java .css .jar etc )
or
2/if it's a file without extension and treat it as a directory and then check if the directory exists in working copy else create one... (6 Replies)
Hi,
Need help in writing a script to read the contents of this file test
Test
00a
00b
00c
00d
00e
00f
where it need to read each line to give a display such as
form meta from dev 00a , config=Striped; add dev 00b:00f to meta 00a
Can any one help me in writing this script (2 Replies)
Dear All
I am trying to write one shell which will be running through Cron which contain one SQL query. But I want to draw/fetch the Username password and Instance name (required to loging to the database) from one single file to run that SQL query . Also this file contain details of multiple... (2 Replies)
hello
have a file1
H87I
Y788O
T347U
J23U
and
file2 J23U U887Y I99U T556U
file3 I99O J99T F557J
file4 N99I T666U R55Y
file5 H87I T347U
file6 H77U R556Y E44T
file7 Y788O K98U H8I
May be using script we can use file1 to search for all the files
and have the output
H87I file5... (3 Replies)
i have done a coding in shell scripting which reads a file line by line and does something....it takes more than 30 seconds to execute for a single file. when i do the same with perl scripting it takes less than a second.
is that shell scripting is not efficient while working with large number of... (1 Reply)