Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


Search this Thread
Old 10-18-2009, 06:37 AM   #1
LQ Newbie
Registered: Jan 2005
Location: Italy
Distribution: Slackware
Posts: 18

Rep: Reputation: 0
Question basic bash help (for, cat, grep): cross reference matching between files

I'm trying to do something very basic in bash. It's a kind of cross-reference matching between 2 files.

I have a file1.txt. It's like this:
I Have a file2.txt. It's like this:
item1       "Properties of item1"
item2       "Properties of item2"
item3       "Properties of item3"
item4       "Properties of item4"
item5       "Properties of item5"
My goal is to print out the lines in file2 that contains lines present in file1.

I do:
for i in $(cat file1.txt); do grep $i file2.txt; done
but I get no output. Will someone please tell me where am I mistaking?


Last edited by NoWone; 10-18-2009 at 10:12 AM.
Old 10-18-2009, 07:04 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
It's working fine for me. Have you made absolutely sure that the two files have identical strings?

BTW, I suggest using a while loop and a redirect instead, so you can avoid calling the external cat program. Also, you should quote your variable expansion and sed expression. Finally, you can add a "^" to the sed expression to ensure that you only match strings that appear at the beginning of a line.

while read i; do grep "^$i" file2.txt; done <file1.txt
Old 10-18-2009, 07:11 AM   #3
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,563

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
Looks like it should work (in bash).
Old 10-18-2009, 10:11 AM   #4
LQ Newbie
Registered: Jan 2005
Location: Italy
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 0

The problem was: file1.txt wasn't in Unix format - it had \r\n s instead of just \n s. Don't ask me why, now it's working with both for and while.

Thank you!


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 06:36 PM
searching within files. cat grep find w3bd3vil Linux - Newbie 4 10-13-2008 04:59 AM
Matching values in a bash script grep, regex's ... ? maxvonseibold Linux - General 6 01-29-2007 07:07 AM
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 03:00 PM
logrotate log files - keeping in order when using grep or cat and bash dmellem Linux - Software 6 02-11-2005 02:05 PM

All times are GMT -5. The time now is 12:56 AM.

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