LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 05: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, 05:40 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Can you post the code that you've got so far?
 
Old 11-22-2011, 05: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, 06:46 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

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


All times are GMT -5. The time now is 10: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration