LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-03-2015, 06:12 PM   #1
Haba2015
LQ Newbie
 
Registered: Mar 2015
Posts: 10

Rep: Reputation: Disabled
How to search multiple files having certain numbers


Hi Everyone,

I have several files in a directory. I want to search these files for specific numbers such as:
71072|71150|71161|71172|71175|71261|71269

These are like unique IDs in each file. They are in column #2. I am using the following code:

for file in *.txt
do
grep -E '71072|71150|71161|71172|71175|71261|71269' <"$file" > /outputdir/"$file"

done

The above code did not do the filtering. When I added -o, it worked but only displayed these unique IDs without other columns.

Help would be appreciated.

Thanks
 
Old 11-03-2015, 06:19 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
The above code did not do the filtering.
Please explain further what this means? Are you saying that without -o you get zero output??
 
Old 11-03-2015, 06:26 PM   #3
Haba2015
LQ Newbie
 
Registered: Mar 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Without -o, I only got back original files. Notting was done on them.
 
Old 11-03-2015, 06:38 PM   #4
jerryleem412
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
grep '71072\|71150\|71161\|71172\|71175\|71261\|71269' "$file" >> /outputdir/"$file"

No need for anything fancy here. The \ is an escape character so the | is interpreted correctly and not as a pipe.

Last edited by jerryleem412; 11-03-2015 at 06:40 PM.
 
Old 11-03-2015, 06:45 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by jerryleem412 View Post
grep '71072\|71150\|71161\|71172\|71175\|71261\|71269' "$file" >> /outputdir/"$file"

No need for anything fancy here. The \ is an escape character so the | is interpreted correctly and not as a pipe.
Pretty sure -E already covers it so you do not have to escape anything.
Quote:
Originally Posted by Haba2015
Without -o, I only got back original files. Notting was done on them.
Still not quite following. You are aware that grep does not change the contents of a file?

Maybe if we could simplify this a little. If we just grab one file that has data in it and at least one of the numbers you are looking for, does the grep then provide output?
Code:
grep -E '71072|71150|71161|71172|71175|71261|71269' file.txt
 
Old 11-03-2015, 07:45 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think he means he got no o/p in destn files. grep -E should work, or
Code:
for file in *.txt
do
    egrep  '71072|71150|71161|71172|71175|71261|71269' $file > /outputdir/$file
done
@OP: no need to use file redirected input '<' or quote filename (unless it has spaces in it).
 
Old 11-03-2015, 09:57 PM   #7
Haba2015
LQ Newbie
 
Registered: Mar 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi Chris,
I tried your code, still the same. Notting performed on the files. The output just like the original files. Thanks
 
Old 11-03-2015, 11:27 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Are you able to provide one of the files of data? Dummy up any information you think is sensitive.

Also, are the files you are working on coming from Windows? If so, try running dos2unix on them prior to using the grep and see if that makes a difference?
 
Old 11-03-2015, 11:39 PM   #9
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Could it be that each input line contains one of these numbers? If the output is identical to the input, that's the only explanation I can think of.
 
Old 11-04-2015, 04:09 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As per the above comments.
You are also aware that grep only searches, it doesn't change anything unlike eg sed?
 
  


Reply

Tags
bash, grep forloop, linux, regex



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
[SOLVED] How to search multiple directories for multiple files? Batistuta_g_2000 Linux - Newbie 4 02-26-2013 11:36 AM
[SOLVED] search and replace across multiple files ottermaton Linux - General 3 06-22-2011 10:46 AM
Search and replace across multiple files zouriel Programming 11 12-07-2007 04:36 PM
search and copy multiple files NiallC Programming 2 04-29-2005 04:40 AM
Search and Replace over multiple files The_Nerd Linux - Software 8 06-20-2004 06:59 AM

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

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