1

Typically when using grep -no every single match has a line number. For example when typing grep -no "[A|a]" chr22.fa I expect:

$ grep -v ">" chr22.fa | grep -o -n "[A|a]" | head -n 17
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210201:A
210202:A
210202:A

However, as of recent the output looks something like:

$ grep -v ">" chr22.fa | grep -o -n "[A|a]" | head -n 17
210201:A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A

Regardless of what I'm searching for and which chromosome txt file I'm dealing with the same issue. Does anyone know why this incorrect format is occurring? Thank you in advance for the help.

Edit: Result of hexdump -C on short file with random sequence of letters:

00000000  41 41 41 41 41 41 41 41  41 41 0a 62 42 42 42 42 |AAAAAAAAAA.bBBBB|
00000010  41 41 41 0a 42 42 41 42  41 41 42 41 42 41 42 42  |AAA.BBABAABABABB|
00000020  41 42 41 0a 41 42 55 57  42 41 55 41 42 57 44 55  |ABA.ABUWBAUABWDU|
00000030  42 57 55 44 55 57 44 55  0a 41 48 55 44 48 47 55  |BWUDUWDU.AHUDHGU|
00000040  45 47 55 0a 68 65 69 66  68 69 65 68 66 0a 77 65  |EGU.heifhiehf.we|
00000050  68 66 69 68 77 65 66 0a  41 41 0a 68 73 65 66 69 |hfihwef.AA.hsefi|
00000060  68 48 41 0a                                       |hHA.|
00000064
15
  • I'm doing the following: grep -no "[A|a]" chr22.fa. Though regardless of what I'm searching for and which chromosome txt file I'm dealing with the outcome is the same. – k99 Jun 13 '20 at 19:24
  • Question updated. The goal isn't to count every match. The goal is to know what line number every single "A" is associated with. – k99 Jun 13 '20 at 19:31
  • 1
    Similar output can happen if there's a carriage return character or a control sequence that moves the cursor around. But [A|a] cannot match any of these, I think. Is this the exact pattern you used? – Kamil Maciorowski Jun 13 '20 at 19:34
  • 1
    grep -V gets grep (BSD grep) 2.5.1-FreeBSD. I typed in grep -on "A" chr22.fa Same result with the bad format. – k99 Jun 13 '20 at 19:53
  • 1
    Sorry. Am I supposed to post it in a different question? As you can tell I'm new here. macOs High Sierra. – k99 Jun 13 '20 at 20:32
2

grep (BSD grep) 2.5.1-FreeBSD
behavior is different from
grep (GNU grep) 3.40

BSD grep will give you line number just once instead of repeating the same line number like GNU grep does

you can install GNU grep with

brew install grep

if you need to use ggrep as default grep you can create a symlink

ln -is $(which ggrep) /usr/local/bin/grep
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.