LinuxQuestions.org
Visit Jeremy's Blog.
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 11-22-2011, 04:09 AM   #1
rebelbuttmunch
LQ Newbie
 
Registered: Sep 2011
Posts: 15

Rep: Reputation: Disabled
script to match patterns on files and echo on the same line


Hi,
I've got two files
file 1:
A hello
B goodbye
C cya

file2
data A data data (lots of data)
data C data data
data B data data

I want to iterate through the first file, taking the first column as the key and grep the second file. I want the output to be merged on one line like this
result:
A Hello data A data data data
B goodbye data data data
C cya data dat data


Im able to do it up to the merging on the one line. The greps Im using are putting everything on its own line like this

A Hello
A Hello data data data

Ideas?
 
Old 11-22-2011, 04:40 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Can you post the code that you've got so far?
 
Old 11-22-2011, 04:58 AM   #3
rebelbuttmunch
LQ Newbie
 
Registered: Sep 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
A colleague solved it for me. Using a file with a column of sorted key values

for seq in `cat seqid_uniq_sort.txt`; do echo `cat file_2 | grep $seq && cat file_1.txt | grep $seq`; done
 
Old 11-22-2011, 05:46 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by rebelbuttmunch View Post
A colleague solved it for me. Using a file with a column of sorted key values

for seq in `cat seqid_uniq_sort.txt`; do echo `cat file_2 | grep $seq && cat file_1.txt | grep $seq`; done
Hi,

thanks for following up and posting the solution. Allow me some comments on the script where I think it can be improved.

Do not use 'for' to read lines of a file. Read the following link that covers the problem in more detail:
http://mywiki.wooledge.org/DontReadLinesWithFor

You do not need to 'cat' the files and then pipe them to grep. Just use
grep "$seq" filename

It is also good practice to double-quote variables.

Finally, here is an alternative that also works with your sample data and does not require a third index file.
Code:
paste <(sort -k2 file2) <(sort -k1 file)
However, the formatting looks a bit different. Your files have trailing blanks which are being kept. If this is not wanted then you could do something like
Code:
paste <(sort -k2 file2) <(sort -k1 file) | sed 's/[[:blank:]]\+/ /g'
The 'sed' will replace multiple SPACE/TAB with a single space.

Hope this helps.
 
  


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
match and combine 2 text files line by line Lowellj Linux - Newbie 9 03-21-2011 08:21 PM
get files that match ALL patterns (using grep?) vigilandy Linux - General 5 06-09-2010 04:18 AM
Python scriptutil for patterns that don't match donnied Programming 8 03-05-2009 12:06 PM
compare two files in C and print the line when they do not match calorie712 Programming 7 04-13-2006 01:56 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM

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

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