Is there a way to join 2 text files sorted by


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there a way to join 2 text files sorted by
# 1  
Old 09-29-2013
Is there a way to join 2 text files sorted by

Can anyone please help me i have 2 text files setup like the one below.

Textfile1:
Code:
randomemail1:randompassword1
randomemail2:randompassword2
randomemail3:randompassword3
randomemail4:randompassword4
randomemail5:randompassword5

Textfile2:
Code:
randompassword1:randomphrase1
randompassword2:randomphrase2
randompassword3:randomphrase3
randompassword4:randomphrase4
randompassword5:randomphrase5

What i am looking to do is search for all instances of randompassword1 from Textfile2 and replace all of the instances with randomphrase1 then once it has been replaced the line/all lines that had the randompassword1 instance is deleted.

Then it would go to randompassword2 and do the same thing so Textfile2 would end up empty and Textfile1 would look like the one below.

Code:
randomemail1:randomphrase1
randomemail2:randomphrase2
randomemail3:randomphrase3
randomemail4:randomphrase4
randomemail5:randomphrase5

Any help on this would be greatly appreciated.

Thank you.

Last edited by Scrutinizer; 09-29-2013 at 08:29 AM.. Reason: code tags
# 2  
Old 09-29-2013
Have you considered the join and the paste utilities?
# 3  
Old 09-29-2013
lol

Quote:
Originally Posted by fpmurphy
Have you considered the join and the paste utilities?
Err i could be finished next year sometime due to the text being over 100000 so could take some time.
# 4  
Old 09-29-2013
These are strange and dangerous requirements, but the following seems to do what you requested. It does, however, at least warn you if two or more of your passwords are the same, if a password or passphrase contains a colon, or if a line with no colon is found in one of your files. Anytime you run this, both of your original input files will be destroyed; I stongly suggest that you make backups before you run this script:
Code:
awk '
BEGIN { FS = OFS = ":" }
NR == 1 { f = FILENAME }
NF != 2 {
        printf("Invalid input from %s line %d deleted: \"%s\"\n", FILENAME, FNR, $0)
        next
}
NR == FNR {
        if($1 in p) 
                printf("Duplicate password \"%s\" from %s line %d deleted.\n",
                        $1, FILENAME, FNR)
        else
                p[$1] = $2
        next
}
{       if($2 in p) $2 = p[$2]
        print > f
}' Textfile2 Textfile1
mv Textfile2 Textfile1

Note that neither input file needs to be sorted for this to work.

If you want to use this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-30-2013
Quote:
Originally Posted by Don Cragun
These are strange and dangerous requirements, but the following seems to do what you requested. It does, however, at least warn you if two or more of your passwords are the same, if a password or passphrase contains a colon, or if a line with no colon is found in one of your files. Anytime you run this, both of your original input files will be destroyed; I stongly suggest that you make backups before you run this script:
Thank you very much for your help mate it is really appreciated Smilie
# 6  
Old 09-30-2013
Is there a way to join 2 text files sorted by

paste -d: Textfile1.txt Textfile2.txt | awk -F: '{A[$2];C[$3]=$4;} END {for (i in A) print i,C[i]}'
This User Gave Thanks to satishmallidi For This Post:
# 7  
Old 09-30-2013
Thanks for the help all instances i have tried so far just look to work ok but never write the output to the textfile i am not sure if i am doing something wrong but.

place Textfile1 and Textfile2 on my backtrack5r3 desktop create a new file sort.py paste in the code and open a console window ./sort.py and it seems to be working but the textfiles are both unchanged when it's finished.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Alignment tool to join text files in 2 directories to create a parallel corpus

I have two directories called English and Hindi. Each directory contains the same number of files with the only difference being that in the case of the English Directory the tag is .english and in the Hindi one the tag is .Hindi The file may contain either a single text or more than one text... (7 Replies)
Discussion started by: gimley
7 Replies

2. Shell Programming and Scripting

How to join 2 text files using bash scripting?

Hi Guys, I want to combine 2 files and and put together in 1 file . See below desired output. Any help will be much appreciated. FILE AX 2134 101L 12345.00 22222.00 1 10 X 2134 101L 12345.00 22222.00 11 20 X 2134 101L 12345.00 22222.00 21 30 X 2134 111L 77777.00 ... (3 Replies)
Discussion started by: H.R
3 Replies

3. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

4. UNIX for Dummies Questions & Answers

How to use the join command to obtain tab delimited text files as an output?

How do you use the join command and obtain tab delimited text files as an output? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

6. UNIX for Dummies Questions & Answers

Insert a line in a sorted text file(s)

Hello, I am trying to add a line (usually just a word) to some text files in a directory that are already sorted. I just don't want to run the sort command again because it can take a long time when the text or log files are really huge. I have a bashscript that will take in the 1st argument... (7 Replies)
Discussion started by: raptor25
7 Replies

7. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

8. UNIX for Dummies Questions & Answers

write a program in c in unix that display the files(includ sub-direc and files within) in a sorted

the sorting is based on name of file, file size modification time stamps o f file it should dislay the output in the following format "." and ".." enteries should be ignored please give some idea how to do it (1 Reply)
Discussion started by: pappu kumar jha
1 Replies

9. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

10. Shell Programming and Scripting

How to Merge / combine / join / paste 2 text files side-by-side

I have 2 text files, both have one simple, single column. The 2 files might be the same length, or might not, and if not, it's unknown which one would be longer. For this example, file1 is longer: ---file1 Joe Bob Mary Sally Fred Elmer David ---file2 Tomato House Car... (3 Replies)
Discussion started by: cajunfries
3 Replies
Login or Register to Ask a Question