LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-14-2012, 04:29 AM   #1
cp167
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
Limit one file with IDs of another file


Hi,

I have two files:

File1:
1 abc def
3 ghi jkl
4 mno pqr
5 stu vwx

File2:
1
5

And I would like to restrict File1 with IDs of File2:

1 abc def
5 stu vwx

Do anyone have an idea and could please help me?
 
Old 09-14-2012, 04:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Try this:
Code:
awk 'BEGIN { while ( ( getline < "file2" ) > 0 ) xz[$1] = $0 } { if ( xz[$1] ) {   print } }' file1
Example:
Code:
$ cat file1
1 abc def
3 ghi jkl
4 mno pqr
5 stu vwx

$ cat file2
1
5

$ awk 'BEGIN { while ( ( getline < "file2" ) > 0 ) xz[$1] = $0 } { if ( xz[$1] ) {   print } }' file1
1 abc def
5 stu vwx
 
1 members found this post helpful.
Old 09-16-2012, 07:11 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
The simplest solution is to just use the file as a pattern list:

Code:
grep -f file2.txt file1.txt
The only problem with this is that it will match numbers found anywhere in the line, not just at the front. But you can use bash's process substitution to pre-filter the list and turn the entries into regex patterns.

Code:
grep -f <( sed -n '/^$/! s/^/^/p' file2.txt ) file1.txt
The sed command adds a "^" to the start of each non-empty line, so that grep will treat is as "<start of line><number>".

Another option would be to loop through the file:

Code:
while read -r num; do
	if [[ $num ]]; then
		grep "^$num" file1.txt
	fi
done <file2.txt
But this does require a separate instance of grep for each line, and so is less efficient.
 
  


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
Too many file open - How can we define Max limit of file descriptor janeshb Linux - Server 11 09-29-2009 11:41 AM
Tcpdump - Capture file whenever file reached the specified limit apit Linux - Networking 5 09-13-2008 04:00 AM
change pci.ids file? feisty steviebee Linux - Laptop and Netbook 1 04-29-2007 10:03 AM
filesize limit on 'cat $file | sed > $file' drkstr Linux - Software 2 07-10-2006 03:47 AM


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