LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-24-2018, 11:40 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
Combine 2 files - command line


I have two files:

1.txt contains

Quote:
email@domain.com:3456312121fdfs34rwrw
2.txt

Quote:
3456312121fdfs34rwrw:true
Desired output.txt

Quote:
email@domain.com:true
How can I create this output using command line (bash)?
Thanks for your support.
 
Old 06-24-2018, 01:54 PM   #2
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 420
Blog Entries: 1

Rep: Reputation: 202Reputation: 202Reputation: 202
dirty code --

file1 -
Code:
# cat file1
email@domain.com:3456312121fdfs34rwrw
email2@domain2.com:whatever
file2 -
Code:
# cat file2
3456312121fdfs34rwrw:true 
whatever:false
Then this abomination will print out what you want in the output -
Code:
for i in $(awk -F: '{print $2}' file1); do echo $(grep $i file1 | awk -F: '{print $1}'):$(grep $i file2| awk -F: '{print $2}') >> file4; done
file4 -
Code:
# cat file4
email@domain.com:true
email2@domain2.com:false
I know it works only when
a> field 2 of file1 matches exactly with field 1 of file2 (no order reqd.)
b> exact number of lines are present in both files.

Edit - Would love to see smarter text/string manipulation logics

Last edited by Honest Abe; 06-24-2018 at 01:56 PM.
 
1 members found this post helpful.
Old 06-24-2018, 02:45 PM   #3
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thanks for your help. It works!
 
Old 06-24-2018, 04:14 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by freeroute View Post
I have two files:
How can I create this output using command line (bash)?
You're still showing zero effort of your own, and have been asking questions like this for some time:
https://www.linuxquestions.org/quest...5/#post5782281

Again, you need to read the LQ Rules about posting homework questions, and the "Question Guidelines" about just asking people to write your scripts for you.
 
Old 06-24-2018, 05:45 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
and those not putting in any effort don't usually get the best solutions.
fwiw, i'd use bash string manipulation for this.
assuming the separator is always a colon ':', and it's always the first and the last word, something along these lines:
Code:
${string1%%:*}:${string2##*:}
http://mywiki.wooledge.org/BashFAQ/100
 
Old 06-24-2018, 06:12 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
if lines in 1.txt and 2.txt match...
Code:
join -t: -1 2 -2 1 -o 1.1,2.2 1.txt 2.txt
 
1 members found this post helpful.
Old 06-24-2018, 06:43 PM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
The OP reeks of homework!
 
Old 06-24-2018, 09:23 PM   #8
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
if lines in 1.txt and 2.txt match...
Code:
join -t: -1 2 -2 1 -o 1.1,2.2 1.txt 2.txt
Thanks for your support.
 
Old 06-25-2018, 07:33 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by freeroute View Post
I have two files:
...
How can I create this output using command line (bash)?
Thanks for your support.
@freeroute,

Please be cognizant of the LQ Site FAQ and how to ask effective questions.

Your thread here, awk - cut usernames from mail addresses with gsub or regex is a very good example of a good question. You described the problem, illustrated what you had done, and then asked for some alternate means to accomplish your goal.

Meanwhile, in this thread, as well as some others, you seem to solely ask, "How do I do ...?" without describing any attempts you have made.

Please recognize that, like yourself, all other LQ members are volunteers and that the site is designed to aid your efforts, while you work alongside your fellow advice givers, to accomplish your goals.
 
1 members found this post helpful.
Old 06-25-2018, 07:35 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by freeroute View Post
Thanks for your support.
Please read the LQ Rules and the "Question Guidelines" before posting such questions again. We don't mind helping, but you just keep asking for us to write your scripts/hand you things.
 
1 members found this post helpful.
  


Reply



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
LXer: Weekly Command: comparing files line by line with diff LXer Syndicated Linux News 0 05-31-2018 05:45 PM
LXer: How to do line-by-line comparison of files in Linux using diff command - Part II LXer Syndicated Linux News 0 01-02-2017 11:20 AM
psql command - how do you combine 2 searches into one command line edomingox Programming 2 05-12-2011 09:12 AM
match and combine 2 text files line by line Lowellj Linux - Newbie 9 03-21-2011 08:21 PM
Command to combine several files as a single file, etc. satimis *BSD 3 06-10-2004 03:59 AM

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

All times are GMT -5. The time now is 06:56 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