LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 03: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, 03:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 06:11 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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



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 10:41 AM
Tcpdump - Capture file whenever file reached the specified limit apit Linux - Networking 5 09-13-2008 03:00 AM
change pci.ids file? feisty steviebee Linux - Laptop and Netbook 1 04-29-2007 09:03 AM
filesize limit on 'cat $file | sed > $file' drkstr Linux - Software 2 07-10-2006 02:47 AM

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

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