Grep highlights on screen but doesn39t redirect to output

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am using grep with -f option.

$grep -f pattern_list.txt data.txt 

On screen, I see the ENTIRE data.txt with the matched patterns (from pattern_list.txt file) highlighted on the screen. So I believe the search is OK. But I want to get ONLY those lines with the patterns in the pattern_list.txt not the entire file with some highlight.

That is because when I try to store the result in a separate file output.txt I get a copy of the data.txt!! This is consistent: whatever result I get on screen is transferred to output.txt and highlights are lost. In any case, I am interested in filtered output with only those lines where patterns match and not highlight.

I tried both redirections with same outcome =>

$grep -f pattern_list.txt data.txt > output.txt

$grep -f pattern_list.txt data.txt >> output.txt

I don t think it is relevant but I am using Linux Mint

/* ********************** */ Jotne asked for example files. The original files are too big. Also I am not sure I can share the contents (they are publicly available and this is not commercial use, but who knows) so I am giving edited versions.

 *   I have checked the behavior is the same with edited versions. 

These are stock names/codes and related data. Two different sets of info are in different files. one group is smaller and other is larger. I took the names out of the smaller group and need corresponding data for those stocks from the other files.

 *   I think this has more to do with the way grep reports -

highlighting vs giving out only those lines in results and not about these specific files. But here are the examples anyway.

pattern_list.txt (There is a space after each name)

ABIRLANUVO 
ACC 
ADANIENT 
ADANIPORTS 
ADANIPOWER 
ALBK 
AMBUJACEM 
ANDHRABANK 
APOLLOHOSP 
APOLLOTYRE 
ARVIND 
ASHOKLEY 
ASIANPAINT 
AUROPHARMA 
AXISBANK 
BAJAJ-AUTO 
BANKBARODA 
BANKINDIA 
BATAINDIA 
BHARATFORG 
BHARTIARTL 
BHEL 
BIOCON 

data.txt

20MICRONS EQ 34.4 34.45 33 33.55 33.4 33.65 77560 2615090.75  604 144J01027 
3MINDIA EQ 4802.35 4900 4721 4845.95 4899 4879.05 363 1750264.35  126 470A01017 
8KMILES EQ 166.05 182 166.05 177.2 174.95 172.15 2155 381502.85  58 650K01013 
A2ZMES EQ 26.3 26.3 26.3 26.3 26.3 25.05 206927 5442180.1  280 619I01012 
AARTIDRUGS EQ 529.15 553.8 509.85 531.9 534.55 529.05 12118 6394851.8  852 767A01016 
AARTIIND EQ 215 218.7 203.1 206.35 203.2 214.85 210984 44316179.1  3499 769A01020 
AARVEEDEN EQ 45.15 49.25 45.1 47 46.45 46.35 3356 157493.35  131 273D01019 
ABAN EQ 815 825 715 736.65 732 809.25 5633124 4204164243.15  154066 421A01028 
ABB EQ 1048.6 1094 1033.45 1062.55 1056.15 1050.6 99591 105627217.75  3025 117A01022 
ABBOTINDIA EQ 2130.05 2189 2101.05 2131.3 2102 2184.3 1354 2915165.8  255 358A01014 
ABCIL EQ 202 209.85 195 201.85 199.05 202.85 28103 5702324.45  998 605B01016 
ABGSHIP EQ 257.1 264 250 258.7 257.3 259.65 61733 15948215.4  2225 067H01016 
ABIRLANUVO EQ 1330 1400.95 1323.2 1370.15 1366.75 1330.85 265037 362100560.5  16500 069A01017 
ACC EQ 1435.1 1487.8 1411 1458.5 1458 1435.55 299862 435694414.85  20261 012A01025 
ACCELYA EQ 701 721 700.1 705.3 703 705.35 11215 7949589.8  906 793A01012 
ACE EQ 28.1 29.45 27.45 28.75 28.9 28.05 543699 15821414.15  1272 731H01025 
ACROPETAL EQ 3.9 4 3.8 4 4 3.9 51927 202669.1  88 055L01013 
ADANIENT EQ 444.1 468.9 423.8 444.55 439.1 444.05 3930067 1762074365.95  62256 423A01024 
ADANIPORTS EQ 267.25 285.4 252.35 270.3 271.05 264.9 9128261 2454070463.55  87896 742F01042 
ADANIPOWER EQ 58.05 62 56.3 59.5 59.45 57.7 20394362 1210636361.9  54378 814H01011 
ADFFOODS EQ 60.15 64.9 58.55 62.9 62.5 62 84037 5233103  1115 982B01019 
ADHUNIK EQ 50.2 52 49.4 50.45 49.9 50.7 115288 5841753.15  815 400H01019 
ADSL EQ 20.75 21.75 20 21.25 21.3 20.75 29963 632240.4  186 102I01027 
ADVANIHOTR EQ 44 46 43 44.05 44.95 45.5 348 15350.8  18 199C01026 
AEGISCHEM EQ 243.95 256.9 239 244.15 242.95 244 35887 8835424.65  1511 208C01017 
AFL EQ 46.5 46.65 44.75 45.85 45.8 47 2738 125420.4  23 020G01017 

/* ******************* */

Edit 2: I don t know why but the lines came up as a bunch down in the description. It seems perfect in the editing window here. Could that be a clue? something to do with newline character???

/* ********************* */

Edit 3: Thanks to perreal s editing my "Edit 2" comment above is taken care of and the lines look separated. A suggestion was to check if grep understands the file as a single line (bunched up like it was seen here around Edit 2 time. So, I performed a basic grep -e like follows:

$grep -e ACC data.txt 

and I got the following two lines as I like:

ACC EQ 1435.1 1487.8 1411 1458.5 1458 1435.55 299862 435694414.85  20261 012A01025 
ACCELYA EQ 701 721 700.1 705.3 703 705.35 11215 7949589.8  906 793A01012  

So, I would think grep sees the file line-by-line under -e option. Just FYI, ACC was highlighted in both the instances on screen. Redirection (>) to a temp.txt was also successful. Only under the -f option grep throws everything with the matched patterns highlighted on screen and a copy in redirection.

One last thing, (for now!) is that the complete file grep -f spews out is line-by-line and not a bunched up : on screen as well as in a redirection. Thanks.

/* ******** */

Edit 4:

Purely out of time crunch I just took the pattern_list.txt and added :

grep -w 

before each line and

data.txt >> output.txt 

after each line.

Saved it as a .sh file, Changed permissions u+x and ran the script. No loops, conditions, nothing. SAD.

I would of course like to understand and solve if possible the grep -f puzzle. Also some pointers on various text formats would be helpful. Because when I ported the output back to LO Calc and added yet another info file to it, I can "see" two identical columns but if I compare them, some corresponding cells are seen as identical but some are not. But those Que and exmpls sm other time/ place. I think it is all related to how text is seen and treated by the software/applications/commands and shown to the user. Thanks.

Answers

For reasons not known to me, the following command works directly. Just FYI.

$fgrep -f pattern_list.txt data.txt 

Not "grep" but "fgrep". Thanks for your interest and time.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/24683128/grep-highlights-on-screen-but-doesnt-redirect-to-output

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils