LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2007, 07:16 AM   #1
thodo
LQ Newbie
 
Registered: Apr 2006
Posts: 4

Rep: Reputation: 0
trouble with grep command


Hi guys,

I have the following problem using a combination of grep and awk commands.
I have an input ascii file (let's call it tmp1) which looks like this:
1234
789
889

and a second file, tmp2, with various columns, in the format:
3838 3328898 789 2398493
1234 3283298 84290 0238438
889 2311201 001 3234389

I would like to match these two files using the FIRST column of the two files
as a reference, i.e. . Example:
If you take the entry in the first line of tmp1 (1234), it matches well
with the second line of tmp2, as they both contain in their first column
the same entry. The same happens for the third line of the two files,
as they both contain the entry 889 in the first column of both files.
If I use a script such as:

for id in $(awk '{print $1}' tmp1)
do
grep [[:space:]]$id[[:space:]]
done

I will also select the first line of tmp2, which I do not want as the value 789
in tmp2 is NOT found in the first column.

Could anyone give some help?

Thanks in advance,

Thodo
 
Old 08-19-2007, 07:24 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'FNR==NR{array[$1]=$0;next}
	 { print array[$0] } ' "tmp2" "tmp1"
 
Old 08-19-2007, 07:27 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
First, your script does not reference the file "tmp2"--looks like you left something out.

Second, why the space before "$id"? If you want to match $id at the beginning of the line, you need "^$id" (or maybe "^\$id")
 
Old 08-19-2007, 07:46 AM   #4
thodo
LQ Newbie
 
Registered: Apr 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
First, your script does not reference the file "tmp2"--looks like you left something out.

Second, why the space before "$id"? If you want to match $id at the beginning of the line, you need "^$id" (or maybe "^\$id")
Hi,

I indeed forgot the tmp2 in the do loop when typing the thread. My files were having spaces before the numbers you see, so the \^ option would not work. In any case, ghostdog74's answer solved my problem.

Thanks to both of you for answering!

Cheers,

Thodo
 
  


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
grep command sgarci Linux - General 3 08-28-2006 07:13 AM
how to use grep command sharonyiisl Linux - Newbie 7 05-28-2006 03:46 PM
cat [file] | grep --- trouble bob_man_uk Linux - General 12 03-10-2006 06:05 AM
grep command in c????? alnreddy Linux - Software 1 12-30-2004 01:01 AM
grep command cuss Linux - General 7 02-14-2003 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

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