LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to search multiple files having certain numbers (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-search-multiple-files-having-certain-numbers-4175557911/)

Haba2015 11-03-2015 06:12 PM

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

grail 11-03-2015 06:19 PM

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??

Haba2015 11-03-2015 06:26 PM

Without -o, I only got back original files. Notting was done on them.

jerryleem412 11-03-2015 06:38 PM

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.

grail 11-03-2015 06:45 PM

Quote:

Originally Posted by jerryleem412 (Post 5444298)
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

chrism01 11-03-2015 07:45 PM

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).

Haba2015 11-03-2015 09:57 PM

Hi Chris,
I tried your code, still the same. Notting performed on the files. The output just like the original files. Thanks

grail 11-03-2015 11:27 PM

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?

berndbausch 11-03-2015 11:39 PM

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.

chrism01 11-04-2015 04:09 PM

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


All times are GMT -5. The time now is 12:09 PM.