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 |
Quote:
|
Without -o, I only got back original files. Notting was done on them.
|
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. |
Quote:
Quote:
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 |
I think he means he got no o/p in destn files. grep -E should work, or
Code:
for file in *.txt |
Hi Chris,
I tried your code, still the same. Notting performed on the files. The output just like the original files. Thanks |
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? |
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.
|
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. |