Compare entries in two files, output one of them in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare entries in two files, output one of them in bash
# 1  
Old 03-10-2009
Compare entries in two files, output one of them in bash

I have two files which are of the following format:

File1:
Unnamed
Unnamed
Boston
Unnamed
New_York
Unnamed

File2:
San_Francisco
Chicago
Portland
Austin
Orlando
Los_Angeles

In the case where an entry in File1 is "Unnamed", I want to output the name from the same line in File2. In the case where the entry in File1 is a name, then I want to output the name from File1.

I want the output to be a file, preferably into File 2, although having the output as File3 would be OK too.

I'm new to using bash, and I can't seem to figure out how to do this. Thanks.
# 2  
Old 03-11-2009
Code:
nawk '
{ if ( FILENAME == "file1" ) _[FNR]=$0
  if ( FILENAME == "file2" && _[FNR] == "Unnamed" )  _[FNR]=$0
}
END { for ( i=1; i<= FNR; i++  ) print _[i] }
' file1 file2 > file3

# 3  
Old 03-11-2009
Code:
nawk 'NR==FNR{_[FNR]=$0;next}{print ($0=="Unnamed")?_[FNR]:$0}' file2 file1 > file3


Last edited by rikxik; 03-11-2009 at 01:26 AM..
# 4  
Old 03-11-2009
Code:
paste -d" " fiel1 file2| awk '{if ($1=="Unnamed"){print $2}else{print $1}}'

# 5  
Old 03-11-2009
Hey all,

I appreciate the responses. I have tried each one and here is some feedback..

pmm: I also have variable names as my filenames in my script. For example:

Code:
nawk '
{ if ( FILENAME == "/files/temp/$1oldNames" ) _[FNR]=$0
  if ( FILENAME == "/files/temp/$1newNames" && _[FNR] == "Unnamed" )  _[FNR]=$0
}
END { for ( i=1; i<= FNR; i++  ) print _[i] }
' /files/temp/$1oldNames /files/temp/$1newNames > /files/temp/$1mergedNames

Even if I harcode the file names I get a blank file3.

rikxik: That just makes file3 just like file1

summer_cherry: This works great except I realized that in file1 and file2, not only does each line have a name, but it also has enough space characters so that each line is 15 characters long, and I need to preserve the spaces (so when I merge it with other data the columns are maintained), or add spaces out to 15 for each line in file3.
# 6  
Old 03-11-2009
Quote:
Originally Posted by forthekill
rikxik: That just makes file3 just like file1
No, it doesn't on my Solaris box:

Code:
$ cat file1
Unnamed
Unnamed
Boston
Unnamed
New_York
Unnamed
$
$ cat file2
San_Francisco
Chicago
Portland
Austin
Orlando
Los_Angeles
$
$ nawk 'NR==FNR{_[FNR]=$0;next}{print ($0=="Unnamed")?_[FNR]:$0}' file2 file1
San_Francisco
Chicago
Boston
Austin
New_York
Los_Angeles

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

BASH - Compare 2 Files, Output All Matches

This is probably rehash but I did look. :rolleyes: I want a bash script that will take Item 1 in File1, traverse all lines in File2 and output if a match exists. Continuing the pattern recursively, Item2, File1, traverse all lines in File2 for a match, continue this pattern until all lines... (6 Replies)
Discussion started by: rcbarr2014
6 Replies

3. UNIX for Dummies Questions & Answers

To compare two files,Output into a new file

Hi Please help me to compare two files and output into a new file file1.txt 15114933 |4001 15291649 |933502 15764675 |4316 15764678 |4316 15761974 |282501 15673104 |933505 15673577 |933505 15673098 |933505 15673096 |933505 15673092 |933505 15760705 ... (13 Replies)
Discussion started by: Ankita Talukdar
13 Replies

4. Shell Programming and Scripting

Compare two files in different formats and get output

File A: DATAFILE TABLESPACE ------------------------- ------------------------- /dev/rprod_0032_011 D_EEM /dev/rprod_0032_012 D_ESO_REF ... (1 Reply)
Discussion started by: Daniel Gate
1 Replies

5. Shell Programming and Scripting

Compare 2 files and output only the different text.

I know the diff does this but it does output more info than just the different text (e.g. $ diff file1 file2 29a30 > /home/alex/Pictures/hello.jpg 1694a1696 > /home/alex/Pictures/hi.jpg ) How can I make it output only /home/alex/Pictures/hello.jpg /home/alex/Pictures/hi.jpg ? thank... (2 Replies)
Discussion started by: hakermania
2 Replies

6. Shell Programming and Scripting

Compare two files and get output

Hi, I have two files, file1 and file2 and I need to compare them by line (exact match, order of the lines is not important) and get output with lines from file2 that are not found in file1 (not other way around). How do I do that? With grep or otherwise.. Thankyou (2 Replies)
Discussion started by: orp56
2 Replies

7. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

8. Shell Programming and Scripting

compare 2 files > output new to third

Hi, I have a question of comparing to files and output the result third file where file1 is the mainfile containing processed dir data and 2nd file grepīs dirīs data again (could be newer dirs comparing file1<file2) now i wanna make shure that output in file3 only contains newer dirs hx... (1 Reply)
Discussion started by: needle
1 Replies

9. Shell Programming and Scripting

Compare 2 files and send output to other

Hi, In File1.txt I have text like: 23AA3424DD23|3423ff25sd5345| and so on In File2.txt I have similar text as File1, but with ",": 23aa3424dd23,192.168.1.100, and so on I wan to remove the pipes from File1 and select 5 fields, then remove "," from File2.txt and select 2 fields (IP's... (14 Replies)
Discussion started by: cameleon
14 Replies

10. UNIX for Dummies Questions & Answers

Compare Files and Output Difference

I have to compare two files for any differences, then output the lab and question number for any differences. This is what I currently have: diff lab2.txt lab2answer.txt > lab2compare.txt Though the output doesn't have to be sent to a .txt (or any sort of log), I found that easier, at least... (2 Replies)
Discussion started by: Joesgrrrl
2 Replies
Login or Register to Ask a Question