-1

I have a text file and I have several numbers inside that. Here are some sample numbers.

+-----------+
|NUMBER     |
+-----------+
| 75705310 |
| 75779034 |
| 75908237 |
| 75681768 |

I need to grep these numbers from another file in my remote server and the output should be as follows with the number and the count,

 1 75964761
 2 75964261

Here is my try, But this will not produce any results.

grep -f /data/session_1.log log_20221001 | sort | uniq -c

But when I use as below I could get the result, But I need to check whole list in session_1.log.

 grep -o '75964761' log_20221001 | sort | uniq -c
      1 75964761

Can someone help me on this issue..

edublog
  • 73
  • 1
  • 8
  • [Check your files](https://unix.stackexchange.com/q/172320/108618) (especially `/data/session_1.log`) against Windows line endings (CRLF), trailing spaces and such. – Kamil Maciorowski Oct 24 '22 at 05:14
  • 1
    Do you have those pipe characters before and after the number? If so, deal with those (and @KamilMaciorowski 's suggestion) by piping your command through `awk '{print $2}' – Stewart Oct 24 '22 at 05:41
  • also `grep -f file | sort | ...` can be reduced to `sort file | ...` – Stewart Oct 24 '22 at 05:42
  • Your example output does not indicate pipe characters in the log file, as shown at the top of the question. Please [edit] your question to show the actual data. The formatting of the input is important if you want to get an answer that will work correctly for you. – Kusalananda Oct 24 '22 at 10:05

1 Answers1

0

Per grep's man page

grep -f /data/session_1.log log_20221001

uses this signature

grep [OPTION...] -f PATTERN_FILE ... [FILE...]

So grep obtains patterns from /data/session_1.log, one per line. Since it's a log, I assume it has data instead of patterns.

Instead try this (I added the last line so you had some data that wasn't unique):

$ cat file
+-----------+
|NUMBER     |
+-----------+
| 75705310 |
| 75779034 |
| 75908237 |
| 75681768 |
| 75681768 |
$ sort file | uniq -c
      2 +-----------+
      2 | 75681768 |
      1 | 75705310 |
      1 | 75779034 |
      1 | 75908237 |
      1 |NUMBER     |
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Stewart
  • 12,628
  • 1
  • 37
  • 80
  • It's unclear what your answer suggests. Should the user just use `sort` and then `uniq -c`? What about the other file then? If there's another file that contains the numbers that they want to investigate, surely the result of the `sort`+`uniq` pipeline needs to be filtered with this? – Kusalananda Oct 24 '22 at 10:09