LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2017, 06:12 PM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
Add new column from another file


Hi,

I have 2 files.

file1 contains by lines: hash:salt:id
file2 contains by lines: username:hash:salt

I would like to add a new coloumn (id) form file1 to file2.

The new file should contains: username:hash:salt:id

Note: file1 contains less rows than file2
I tried
Code:
paste -d':' file1 file2
, but this solution not good for me.

Thank you

Last edited by freeroute; 08-02-2017 at 06:22 PM.
 
Old 08-02-2017, 06:26 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Use join, not paste.
 
1 members found this post helpful.
Old 08-02-2017, 06:37 PM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Use join, not paste.
Tried, but this command does not work:
Quote:
join <(cat file1.txt | sort ) <(cat file2.txt | sort ) >final_file3.txt
I guess because of file1 contains less lines(users).
 
Old 08-02-2017, 06:54 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Guess again.
You need to read the manpage - all of it, carefully. Join will join appropriate lines, and ignore unmatched.
 
1 members found this post helpful.
Old 08-02-2017, 06:56 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,708

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
from man join
Quote:
join [OPTION]... FILE1 FILE2
Please review the man document re: OPTIONs to see which apply to your case.

Edit: I shouldn't take so long to compose, I guess...what syg00 said, too.

Last edited by scasey; 08-02-2017 at 06:57 PM.
 
1 members found this post helpful.
Old 08-02-2017, 09:47 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
You probably also need to feed join with properly sorted data and sort on the field to be joined:

Code:
join ... <(sort -t: -k1,1 file1) <(sort -t: -k2,2 file2)

Last edited by Turbocapitalist; 08-03-2017 at 02:04 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
add column to file under header bash bishop2001 Programming 12 11-30-2015 11:35 AM
[SOLVED] add 250 single column files to create one file wilelucho Programming 10 06-27-2012 02:05 PM
[SOLVED] Add a column to a file. Sha_unix Linux - Newbie 9 11-23-2011 04:08 PM
[SOLVED] Need to add a column to a text file Thaidog Programming 2 04-28-2011 09:09 AM
[SOLVED] how to add blank lines whenever the value of a given column alters in a data file? vcmota Programming 9 01-11-2011 08:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration