Outputting from two input files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Outputting from two input files.
# 1  
Old 04-08-2007
Outputting from two input files.

Ok, lets suppose I have two files like so:

file1

John 5441223
Sandy 113446
Jill 489799

file2

Sandy Tuesday
Jill Friday
John Monday

Is it possible to match records from these two files and output them into one output file? For example, lets suppose I want to output like this:

John Monday 5441223
Sandy Tuesday 113446
etc...

Preferably by looping if possible so I don't have sloppy code.
# 2  
Old 04-09-2007
No loop needed
Code:
$ cat file1
John 5441223
Sandy 113446
Jill 489799
$ cat file2
Sandy Tuesday
Jill Friday
John Monday
$
$
$ sort < file1 > file1.sorted
$ sort < file2 > file2.sorted
$
$ join file2.sorted file1.sorted
Jill Friday 489799
John Monday 5441223
Sandy Tuesday 113446
$

# 3  
Old 04-09-2007
Thanks for the info. I figured this but any way to do with grep and looping because I'm using a very large data set. Thanks.
# 4  
Old 04-09-2007
"grep and looping"? That would not be efficient. grep has to read the entire file. This is faster.
# 5  
Old 04-09-2007
So it's possible, but less efficient? How would we do that anyway, say, with a loop like this:

for ******
do
******
done > loop.out

Last edited by Liguidsoul; 04-09-2007 at 01:11 AM..
# 6  
Old 04-10-2007
Quote:
Originally Posted by Liguidsoul
So it's possible, but less efficient? How would we do that anyway, say, with a loop like this:

for ******
do
******
done > loop.out
Code:
while read nm day
do
  grep $nm file1 | read name no
  echo $name $day $no
done <file2

or

Code:
awk ' { arr[$1] = arr[$1] ? arr[$1]" "$2 : $2 }
END {
     for( key in arr ) { print key " " arr[key] }
}' file2 file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing fastq files and outputting common records

I have two files: File_1: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCAGAAGCAGCAT + GGGGGGGGGGGGGGGGGCCGGGGGF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8F ... (3 Replies)
Discussion started by: Xterra
3 Replies

2. UNIX for Dummies Questions & Answers

Outputting 1 file per row if pattern exists between files

I have many files that can have various amounts of rows. I essentially want to output each row into a new file if a pattern is matched between two files. I have some code that does something similar but I want it to output every single input row from every file into a separate output file; that... (5 Replies)
Discussion started by: verse123
5 Replies

3. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. UNIX for Dummies Questions & Answers

rm command-outputting files as they are deleted?

Solaris 10/Korn Hi unix experts!, Is it possible to output the actual file names to a file as they are being deleted via the rm command? Context: Im executing the shell script at the command line and directing the output to an output file eg purgescript.ksh > output.lst within the... (3 Replies)
Discussion started by: satnamx
3 Replies

6. Shell Programming and Scripting

write a perl script or kornshell reading a two files and outputting to comma format

Hello Can someone help me to write a perl script or kornshell reading a two files and outputting to comma format. Here is the two files listofdisks.txt id, diskname, diskgroup, diskisze(GB), FC 1, CN34, GRP1, 30, FC_CN34 2, CN67, GRP5, 19, 4, VD1, GRP4, 23, FC_VD1 6, CF_D1, ... (0 Replies)
Discussion started by: deiow
0 Replies

7. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

8. UNIX for Dummies Questions & Answers

getting input, then outputting it

Hi! I am a newbie to Unix. I was writing a little game program for fun when thought of an idea to allow data to be saved. I knew to take all of the Predefined variables and put them into a separate file, then including the file in the program. But I am having trouble making it so that the user... (0 Replies)
Discussion started by: signebedi
0 Replies

9. Shell Programming and Scripting

Help in outputting the result in log files

This is the file am having: "40","1G1AL55 ",30482,9000 "40","1G1ZT58 ",29098,10600 "40","1G1AL15 ",29222,9400 "46","1G6KD57 ",3083,28400 "46","1G6KD57 ",27909,25200 "49","1G1ZU57 ",16391,13900 "49","1G2ZG58 ",28856,12400 I want to display the output in three files... (23 Replies)
Discussion started by: dave_nithis
23 Replies

10. Shell Programming and Scripting

sed not outputting last line of input file

I am using sed for a simple substitution (see command syntax below). Everything works fine except that the last line of the input file does not get written to the output file. Has anyone ever seen this and know of way to force the last line to be written? I don't know if it's playing a part in... (3 Replies)
Discussion started by: 2reperry
3 Replies
Login or Register to Ask a Question